Commit 2b7473e1 authored by Vladimir Paun's avatar Vladimir Paun
Browse files

readme ex

parent 6e356dd5
Hello
=====
This is an exercise used to learn Test-Driven Development and unit testing in Python.
Feel free to contribute!
## Rationale
Binary numbers are rather easy to understand, even if becoming familiar with them requires some time.
# Section 1
\ No newline at end of file
The package we are going to write will provide a class that represents binary numbers (`Binary`) and a class that represents binary numbers with a given bit size (`SizeBinary`). They shall provide basic binary operations like logical (and, or, xor), arithmetic (addition, subtraction, multiplication, division), shifts and indexing.
A quick example of what the package shall do:
``` python
>>> b = Binary('0101110001')
>>> hex(b)
'0x171'
>>> int(b)
369
>>> b[0]
'1'
>>> b[9]
'0'
>>> b.SHR()
'10111000'
```
Run with:
```
python -m unittest test_name
```
## Python and bases
Binary system is just a representation of numbers with base 2, just like hexadecimal (base 16) and decimal (base 10). Python can already natively deal with different bases, even if internally numbers are always stored as decimal integers. Let us check it
``` python
>>> a = 5
>>> a
5
>>> a = 0x5
>>> a
5
>>> a = 0b101
>>> a
5
>>> hex(0b101)
'0x5'
>>> bin(5)
'0b101'
```
As you can see Python understands some common bases out of the box, using the `0x` prefix for hexadecimal numbers and the `0b` for binary ones (and `0o`) for octals). However the number is always printed in its base-10 form (`5` in this case). This means however that a binary number cannot be indexed, since integers does not provide support for this operation
``` python
>>> 0b101[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
```
You can also use a different base when converting things to integers, through the `base` parameter
``` python
>>> a = int('101', base=2)
>>> a
5
>>> a = int('10', base=5)
>>> a
5
```
## Test-driven development
Simple tasks are the best way to try and use new development methodologies, so this is a good occasion to start working with the so-called test-driven approach. Test-driven Development (TDD) basically means that the first thing you do when developing is to write some tests, that is programs that use what you are going to develop. The purpose of those programs is to test that your final product complies with a given behaviour. So they provide
* **Documentation** for your API: they are examples of use of your package.
* **Regression checks**: when you change the code to develop new features they shall not break the behaviour of the previous package versions.
* **TODO list**: until all tests run successfully you have something still waiting to be implemented.
## Randomness
The `random` module is tested through the properties of the output it generates (it's not test-driven as it is a standard module provided by the runtime.) For example, sampled elements from a list must belong to the list while shuffling a list should not lose any contained element.
Run with:
```
python -m unittest test_random
```
\ No newline at end of file
import random
import unittest
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.seq = range(10)
# def setUp(self):
# self.seq = range(10)
def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1,2,3))
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
if __name__ == '__main__':
unittest.main()
pass
# # make sure the shuffled sequence does not lose any elements
# random.shuffle(self.seq)
# self.seq.sort()
# self.assertEqual(self.seq, range(10))
# # should raise an exception for an immutable sequence
# self.assertRaises(TypeError, random.shuffle, (1,2,3))
# def test_choice(self):
# element = random.choice(self.seq)
# self.assertTrue(element in self.seq)
# def test_sample(self):
# with self.assertRaises(ValueError):
# random.sample(self.seq, 20)
# for element in random.sample(self.seq, 5):
# self.assertTrue(element in self.seq)
# if __name__ == '__main__':
# unittest.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment