def test_sizeof(self): self.assertEqual(sizeof('int'), 4) define('INIT_THREAD_SIZE', 2048 * sizeof('long')) self.assertEqual(cstruct.DEFINES['INIT_THREAD_SIZE'], 16384) self.assertEqual(sizeof('struct Position'), 3) self.assertEqual(sizeof('struct Position'), len(Position)) self.assertEqual(sizeof(Position), 3) self.assertRaises(KeyError, lambda: sizeof('bla')) self.assertRaises(KeyError, lambda: sizeof('struct Bla'))
from cstruct import define, typedef, CStruct from keyplus.constants import * class CStructWithBytes(CStruct): def to_bytes(self): return bytearray(self.pack()) typedef('uint8', 'uint8_t') typedef('uint16', 'uint16_t') typedef('uint32', 'uint32_t') typedef('int8', 'int8_t') typedef('int16', 'int16_t') typedef('int32', 'int32_t') define('MAX_NUM_KEYBOARDS', 64) define('MAX_NUM_DEVICES', 64) define('AES_KEY_LEN', 16) define('NRF_ADDR_LEN', 5) def make_bit_field_variables(class_obj, field_list): """ This function uses the python built-in `property()` to create attributes to access the bit fields of a function directly. The field_list should contain a list of properties where each property is defined as a tuple of the form: (bit_mask_variable, bit_field_name, bit_field_mask) After this function has been called, you should be able to get and set the bit fields using the `bit_field_name`.
def test_define(self): define('A', 10) self.assertEqual(cstruct.DEFINES['A'], 10) undef('A') self.assertRaises(KeyError, lambda: cstruct.DEFINES['A'])
# all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. # #***************************************************************************** from unittest import TestCase, main from cstruct import (sizeof, typedef, define, CStruct, NATIVE_ORDER) define("UT_NAMESIZE", 32) define("UT_LINESIZE", 32) define("UT_HOSTSIZE", 256) typedef("int", "pid_t") typedef("long", "time_t") class ExitStatus(CStruct): __def__ = """ struct { short e_termination; /* Process termination status. */ short e_exit; /* Process exit status. */ } """
import struct class TestUnion(cstruct.CStruct): __byte_order__ = cstruct.LITTLE_ENDIAN __def__ = """ union { uint8 a; uint8 a1; uint16 b; uint32 c; struct Partition d; struct Partition e[4]; } """ define('INIT_THREAD_SIZE', 2048 * sizeof('long')) # class TaskUnion(cstruct.CStruct): # __def__ = """ # union { # struct task_struct task; # unsigned long stack[INIT_TASK_SIZE/sizeof(long)]; # } # """ class TestCaseUnion(TestCase): # def test_sizeof(self): # self.assertEqual(cstruct.DEFINES['INIT_THREAD_SIZE'], 16384) # self.assertEqual(sizeof('struct TestUnion'), 64) #