drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
bitfield.py
1#!/usr/bin/env python3
2#
3# Drizzle Client & Protocol Library
4#
5# Copyright (C) 2008 Eric Day ([email protected])
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are
10# met:
11#
12# * Redistributions of source code must retain the above copyright
13# notice, this list of conditions and the following disclaimer.
14#
15# * Redistributions in binary form must reproduce the above
16# copyright notice, this list of conditions and the following disclaimer
17# in the documentation and/or other materials provided with the
18# distribution.
19#
20# * The names of its contributors may not be used to endorse or
21# promote products derived from this software without specific prior
22# written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35#
36
37import unittest
38
39class BitField(object):
40 '''Base class for managing bitfields.'''
41
42 _fields = []
43
44 def __init__(self, value=0):
45 self._value = value
46
47 def __getattr__(self, name):
48 try:
49 if name.isupper():
50 return 1 << self._fields.index(name)
51 elif name.islower():
52 return self._value & (1 << self._fields.index(name.upper())) != 0
53 raise Exception()
54 except Exception:
55 raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
56
57 def __setattr__(self, name, value):
58 try:
59 if name[0] == '_':
60 self.__dict__[name] = value
61 else:
62 if name.islower():
63 if value:
64 self._value |= (1 << self._fields.index(name.upper()))
65 else:
66 self._value &= ~(1 << self._fields.index(name.upper()))
67 else:
68 raise Exception()
69 except Exception:
70 raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
71
72 def __str__(self):
73 return str([(self._fields[x], 1 << x) for x in range(0, len(self._fields)) if (1 << x) & self._value])
74
75 def value(self):
76 return self._value
77
79 _fields = [
80 'READ',
81 'WRITE',
82 'CREATE',
83 'DIRECT'
84 ]
85
86class TestField(unittest.TestCase):
87
88 def testDefaultInit(self):
89 f = ExampleField()
90 self.assertEqual(f.value(), 0)
91
92 def testDataInit(self):
93 f = ExampleField(15)
94 self.assertEqual(f.value(), 15)
95
96 def testGetAttr(self):
97 f = ExampleField(1)
98 self.assertEqual(f.read, True)
99 self.assertEqual(f.READ, 1)
100 self.assertEqual(f.write, False)
101 self.assertEqual(f.WRITE, 2)
102
103 def testBadGetAttr(self):
104 f = ExampleField()
105 self.assertRaises(AttributeError, getattr, f, 'BAD')
106 self.assertRaises(AttributeError, getattr, f, 'bad')
107 self.assertRaises(AttributeError, getattr, f, 'Read')
108
109 def testSetAttr(self):
110 f = ExampleField()
111 self.assertEqual(f.read, False)
112 self.assertEqual(f.write, False)
113 f.read = True
114 self.assertEqual(f.read, True)
115 self.assertEqual(f.write, False)
116
117 def testBadSetAttr(self):
118 f = ExampleField()
119 self.assertRaises(AttributeError, setattr, f, 'BAD', 0)
120 self.assertRaises(AttributeError, setattr, f, 'bad', 0)
121 self.assertRaises(AttributeError, setattr, f, 'Read', 0)
122 self.assertRaises(AttributeError, setattr, f, 'READ', 0)
123
124if __name__ == '__main__':
125 unittest.main()