blob: 34dfd83396b257841c76835a14da52e99747565f [file] [log] [blame]
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01001#!/usr/bin/env python
2from __future__ import print_function
3
Ivan Smirnovd0bafd92016-06-26 16:35:28 +01004import unittest
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01005import numpy as np
Ivan Smirnovbdc99022016-06-19 16:54:07 +01006from example import (
Ivan Smirnov669e1422016-06-21 21:05:29 +01007 create_rec_simple, create_rec_packed, create_rec_nested, print_format_descriptors,
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +01008 print_rec_simple, print_rec_packed, print_rec_nested, print_dtypes, get_format_unbound,
Ivan Smirnovf9c0def2016-07-20 00:19:24 +01009 create_rec_partial, create_rec_partial_nested, create_string_array, print_string_array
Ivan Smirnovbdc99022016-06-19 16:54:07 +010010)
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010011
12
13def check_eq(arr, data, dtype):
14 np.testing.assert_equal(arr, np.array(data, dtype=dtype))
15
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010016unittest.TestCase().assertRaisesRegex(
17 RuntimeError, 'unsupported buffer format', get_format_unbound)
18
Ivan Smirnovbdc99022016-06-19 16:54:07 +010019print_format_descriptors()
Ivan Smirnov4f164212016-06-22 01:07:20 +010020print_dtypes()
Ivan Smirnovbdc99022016-06-19 16:54:07 +010021
Ivan Smirnov8502f542016-06-19 16:09:44 +010022simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
23 'formats': ['?', 'u4', 'f4'],
24 'offsets': [0, 4, 8]})
25packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')])
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010026
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010027elements = [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)]
28
Ivan Smirnov8502f542016-06-19 16:09:44 +010029for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010030 arr = func(0)
31 assert arr.dtype == dtype
32 check_eq(arr, [], simple_dtype)
33 check_eq(arr, [], packed_dtype)
34
Ivan Smirnov8502f542016-06-19 16:09:44 +010035 arr = func(3)
36 assert arr.dtype == dtype
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010037 check_eq(arr, elements, simple_dtype)
38 check_eq(arr, elements, packed_dtype)
Ivan Smirnov2e1565e2016-06-19 16:05:23 +010039
Ivan Smirnov2a7acb62016-06-22 00:33:56 +010040 if dtype == simple_dtype:
41 print_rec_simple(arr)
42 else:
43 print_rec_packed(arr)
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010044
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010045
46arr = create_rec_partial(3)
47print(arr.dtype)
48partial_dtype = arr.dtype
49assert '' not in arr.dtype.fields
50assert partial_dtype.itemsize > simple_dtype.itemsize
51check_eq(arr, elements, simple_dtype)
52check_eq(arr, elements, packed_dtype)
53
54arr = create_rec_partial_nested(3)
55print(arr.dtype)
56assert '' not in arr.dtype.fields
57assert '' not in arr.dtype.fields['a'][0].fields
58assert arr.dtype.itemsize > partial_dtype.itemsize
59np.testing.assert_equal(arr['a'], create_rec_partial(3))
60
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010061nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])
62
63arr = create_rec_nested(0)
64assert arr.dtype == nested_dtype
65check_eq(arr, [], nested_dtype)
66
67arr = create_rec_nested(3)
68assert arr.dtype == nested_dtype
69check_eq(arr, [((False, 0, 0.0), (True, 1, 1.5)),
70 ((True, 1, 1.5), (False, 2, 3.0)),
71 ((False, 2, 3.0), (True, 3, 4.5))], nested_dtype)
Ivan Smirnov2a7acb62016-06-22 00:33:56 +010072print_rec_nested(arr)
Ivan Smirnov41c33992016-07-18 19:58:20 +010073
74assert create_rec_nested.__doc__.strip().endswith('numpy.ndarray[dtype=NestedStruct]')
Ivan Smirnovf9c0def2016-07-20 00:19:24 +010075
76arr = create_string_array(True)
77print(arr.dtype)
78print_string_array(arr)
79dtype = arr.dtype
80assert arr['a'].tolist() == [b'', b'a', b'ab', b'abc']
81assert arr['b'].tolist() == [b'', b'a', b'ab', b'abc']
82arr = create_string_array(False)
83assert dtype == arr.dtype