blob: c581cf3f72195e3a9525ac77c289dfd771dcdb67 [file] [log] [blame]
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01001#!/usr/bin/env python
2from __future__ import print_function
3
4import numpy as np
Ivan Smirnovbdc99022016-06-19 16:54:07 +01005from example import (
Ivan Smirnov669e1422016-06-21 21:05:29 +01006 create_rec_simple, create_rec_packed, create_rec_nested, print_format_descriptors,
Ivan Smirnov4f164212016-06-22 01:07:20 +01007 print_rec_simple, print_rec_packed, print_rec_nested, print_dtypes
Ivan Smirnovbdc99022016-06-19 16:54:07 +01008)
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01009
10
11def check_eq(arr, data, dtype):
12 np.testing.assert_equal(arr, np.array(data, dtype=dtype))
13
Ivan Smirnovbdc99022016-06-19 16:54:07 +010014print_format_descriptors()
Ivan Smirnov4f164212016-06-22 01:07:20 +010015print_dtypes()
Ivan Smirnovbdc99022016-06-19 16:54:07 +010016
Ivan Smirnov8502f542016-06-19 16:09:44 +010017simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
18 'formats': ['?', 'u4', 'f4'],
19 'offsets': [0, 4, 8]})
20packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')])
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010021
Ivan Smirnov8502f542016-06-19 16:09:44 +010022for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010023 arr = func(0)
24 assert arr.dtype == dtype
25 check_eq(arr, [], simple_dtype)
26 check_eq(arr, [], packed_dtype)
27
Ivan Smirnov8502f542016-06-19 16:09:44 +010028 arr = func(3)
29 assert arr.dtype == dtype
30 check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], simple_dtype)
31 check_eq(arr, [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)], packed_dtype)
Ivan Smirnov2e1565e2016-06-19 16:05:23 +010032
Ivan Smirnov2a7acb62016-06-22 00:33:56 +010033 if dtype == simple_dtype:
34 print_rec_simple(arr)
35 else:
36 print_rec_packed(arr)
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010037
38nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])
39
40arr = create_rec_nested(0)
41assert arr.dtype == nested_dtype
42check_eq(arr, [], nested_dtype)
43
44arr = create_rec_nested(3)
45assert arr.dtype == nested_dtype
46check_eq(arr, [((False, 0, 0.0), (True, 1, 1.5)),
47 ((True, 1, 1.5), (False, 2, 3.0)),
48 ((False, 2, 3.0), (True, 3, 4.5))], nested_dtype)
Ivan Smirnov2a7acb62016-06-22 00:33:56 +010049print_rec_nested(arr)