Skip to content

Commit

Permalink
Add float and double fields
Browse files Browse the repository at this point in the history
  • Loading branch information
schmichael committed Sep 29, 2011
1 parent 3319d24 commit 46e4961
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mmstats.py
Expand Up @@ -266,6 +266,16 @@ class ByteField(ReadWriteField):
buffer_type = ctypes.c_byte


class FloatField(BufferedDescriptorField):
"""32bit Float Field"""
buffer_type = ctypes.c_float


class DoubleField(BufferedDescriptorField):
"""64bit Double Precision Float Field"""
buffer_type = ctypes.c_double


class BoolField(ReadWriteField):
"""Boolean Field"""
# Avoid potential ambiguity and marshal bools to 0/1 manually
Expand Down
19 changes: 19 additions & 0 deletions tests/test_types.py
Expand Up @@ -130,3 +130,22 @@ class SimpleCounter(mmstats.MmStats):
s.counter.inc(-2)
self.assertNotEqual(s.counter.value, -1)

def test_floats(self):
class FloatTest(mmstats.MmStats):
f = mmstats.FloatField()
d = mmstats.DoubleField()

ft = FloatTest(filename='mmstats-test_floats')
self.assertEqual(ft.f, 0.0)
self.assertEqual(ft.d, 0.0)
ft.d = ft.f = 1.0
self.assertEqual(ft.f, 1.0)
self.assertEqual(ft.d, 1.0)
ft.d = ft.f = -1.0
self.assertEqual(ft.f, -1.0)
self.assertEqual(ft.d, -1.0)
ft.d = ft.f = 1.0 / 3
self.assertTrue(ft.f > 0.3)
self.assertTrue(ft.d > 0.3)
self.assertTrue(ft.f < 0.4)
self.assertTrue(ft.d < 0.4)

0 comments on commit 46e4961

Please sign in to comment.