blob: e0a001804099125c198adf66e7f616a6f4ee2110 [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 Smirnovd0bafd92016-06-26 16:35:28 +01008 print_rec_simple, print_rec_packed, print_rec_nested, print_dtypes, get_format_unbound
Ivan Smirnovbdc99022016-06-19 16:54:07 +01009)
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010010
11
12def check_eq(arr, data, dtype):
13 np.testing.assert_equal(arr, np.array(data, dtype=dtype))
14
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010015unittest.TestCase().assertRaisesRegex(
16 RuntimeError, 'unsupported buffer format', get_format_unbound)
17
Ivan Smirnovbdc99022016-06-19 16:54:07 +010018print_format_descriptors()
Ivan Smirnov4f164212016-06-22 01:07:20 +010019print_dtypes()
Ivan Smirnovbdc99022016-06-19 16:54:07 +010020
Ivan Smirnov8502f542016-06-19 16:09:44 +010021simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
22 'formats': ['?', 'u4', 'f4'],
23 'offsets': [0, 4, 8]})
24packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')])
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010025
Ivan Smirnov8502f542016-06-19 16:09:44 +010026for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010027 arr = func(0)
28 assert arr.dtype == dtype
29 check_eq(arr, [], simple_dtype)
30 check_eq(arr, [], packed_dtype)
31
Ivan Smirnov8502f542016-06-19 16:09:44 +010032 arr = func(3)
33 assert arr.dtype == dtype
34 check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], simple_dtype)
35 check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], packed_dtype)
Ivan Smirnov2e1565e2016-06-19 16:05:23 +010036
Ivan Smirnov2a7acb62016-06-22 00:33:56 +010037 if dtype == simple_dtype:
38 print_rec_simple(arr)
39 else:
40 print_rec_packed(arr)
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010041
42nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])
43
44arr = create_rec_nested(0)
45assert arr.dtype == nested_dtype
46check_eq(arr, [], nested_dtype)
47
48arr = create_rec_nested(3)
49assert arr.dtype == nested_dtype
50check_eq(arr, [((False, 0, 0.0), (True, 1, 1.5)),
51 ((True, 1, 1.5), (False, 2, 3.0)),
52 ((False, 2, 3.0), (True, 3, 4.5))], nested_dtype)
Ivan Smirnov2a7acb62016-06-22 00:33:56 +010053print_rec_nested(arr)