• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc.  All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8"""Test use of numpy types with repeated and non-repeated scalar fields."""
9
10import unittest
11
12import numpy as np
13
14from google.protobuf.internal import testing_refleaks
15from google.protobuf import unittest_pb2
16
17message = unittest_pb2.TestAllTypes()
18np_float_scalar = np.float64(0.0)
19np_1_float_array = np.zeros(shape=(1,), dtype=np.float64)
20np_2_float_array = np.zeros(shape=(2,), dtype=np.float64)
21np_11_float_array = np.zeros(shape=(1, 1), dtype=np.float64)
22np_22_float_array = np.zeros(shape=(2, 2), dtype=np.float64)
23
24np_int_scalar = np.int64(0)
25np_1_int_array = np.zeros(shape=(1,), dtype=np.int64)
26np_2_int_array = np.zeros(shape=(2,), dtype=np.int64)
27np_11_int_array = np.zeros(shape=(1, 1), dtype=np.int64)
28np_22_int_array = np.zeros(shape=(2, 2), dtype=np.int64)
29
30np_uint_scalar = np.uint64(0)
31np_1_uint_array = np.zeros(shape=(1,), dtype=np.uint64)
32np_2_uint_array = np.zeros(shape=(2,), dtype=np.uint64)
33np_11_uint_array = np.zeros(shape=(1, 1), dtype=np.uint64)
34np_22_uint_array = np.zeros(shape=(2, 2), dtype=np.uint64)
35
36np_bool_scalar = np.bool_(False)
37np_1_bool_array = np.zeros(shape=(1,), dtype=np.bool_)
38np_2_bool_array = np.zeros(shape=(2,), dtype=np.bool_)
39np_11_bool_array = np.zeros(shape=(1, 1), dtype=np.bool_)
40np_22_bool_array = np.zeros(shape=(2, 2), dtype=np.bool_)
41
42
43@testing_refleaks.TestCase
44class NumpyIntProtoTest(unittest.TestCase):
45
46  # Assigning dim 1 ndarray of ints to repeated field should pass
47  def testNumpyDim1IntArrayToRepeated_IsValid(self):
48    message.repeated_int64[:] = np_1_int_array
49    message.repeated_int64[:] = np_2_int_array
50
51    message.repeated_uint64[:] = np_1_uint_array
52    message.repeated_uint64[:] = np_2_uint_array
53
54  # Assigning dim 2 ndarray of ints to repeated field should fail
55  def testNumpyDim2IntArrayToRepeated_RaisesTypeError(self):
56    with self.assertRaises(TypeError):
57      message.repeated_int64[:] = np_11_int_array
58    with self.assertRaises(TypeError):
59      message.repeated_int64[:] = np_22_int_array
60
61    with self.assertRaises(TypeError):
62      message.repeated_uint64[:] = np_11_uint_array
63    with self.assertRaises(TypeError):
64      message.repeated_uint64[:] = np_22_uint_array
65
66  # Assigning any ndarray of floats to repeated int field should fail
67  def testNumpyFloatArrayToRepeated_RaisesTypeError(self):
68    with self.assertRaises(TypeError):
69      message.repeated_int64[:] = np_1_float_array
70    with self.assertRaises(TypeError):
71      message.repeated_int64[:] = np_11_float_array
72    with self.assertRaises(TypeError):
73      message.repeated_int64[:] = np_22_float_array
74
75  # Assigning any np int to scalar field should pass
76  def testNumpyIntScalarToScalar_IsValid(self):
77    message.optional_int64 = np_int_scalar
78    message.optional_uint64 = np_uint_scalar
79
80  # Assigning any ndarray of ints to scalar field should fail
81  def testNumpyIntArrayToScalar_RaisesTypeError(self):
82    with self.assertRaises(TypeError):
83      message.optional_int64 = np_1_int_array
84    with self.assertRaises(TypeError):
85      message.optional_int64 = np_11_int_array
86    with self.assertRaises(TypeError):
87      message.optional_int64 = np_22_int_array
88
89    with self.assertRaises(TypeError):
90      message.optional_uint64 = np_1_uint_array
91    with self.assertRaises(TypeError):
92      message.optional_uint64 = np_11_uint_array
93    with self.assertRaises(TypeError):
94      message.optional_uint64 = np_22_uint_array
95
96  # Assigning any ndarray of floats to scalar field should fail
97  def testNumpyFloatArrayToScalar_RaisesTypeError(self):
98    with self.assertRaises(TypeError):
99      message.optional_int64 = np_1_float_array
100    with self.assertRaises(TypeError):
101      message.optional_int64 = np_11_float_array
102    with self.assertRaises(TypeError):
103      message.optional_int64 = np_22_float_array
104
105
106@testing_refleaks.TestCase
107class NumpyFloatProtoTest(unittest.TestCase):
108
109  # Assigning dim 1 ndarray of floats to repeated field should pass
110  def testNumpyDim1FloatArrayToRepeated_IsValid(self):
111    message.repeated_float[:] = np_1_float_array
112    message.repeated_float[:] = np_2_float_array
113
114  # Assigning dim 2 ndarray of floats to repeated field should fail
115  def testNumpyDim2FloatArrayToRepeated_RaisesTypeError(self):
116    with self.assertRaises(TypeError):
117      message.repeated_float[:] = np_11_float_array
118    with self.assertRaises(TypeError):
119      message.repeated_float[:] = np_22_float_array
120
121  # Assigning any np float to scalar field should pass
122  def testNumpyFloatScalarToScalar_IsValid(self):
123    message.optional_float = np_float_scalar
124
125  # Assigning any ndarray of float to scalar field should fail
126  def testNumpyFloatArrayToScalar_RaisesTypeError(self):
127    with self.assertRaises(TypeError):
128      message.optional_float = np_1_float_array
129    with self.assertRaises(TypeError):
130      message.optional_float = np_11_float_array
131    with self.assertRaises(TypeError):
132      message.optional_float = np_22_float_array
133
134
135@testing_refleaks.TestCase
136class NumpyBoolProtoTest(unittest.TestCase):
137
138  # Assigning dim 1 ndarray of bool to repeated field should pass
139  def testNumpyDim1BoolArrayToRepeated_IsValid(self):
140    message.repeated_bool[:] = np_1_bool_array
141    message.repeated_bool[:] = np_2_bool_array
142
143  # Assigning dim 2 ndarray of bool to repeated field should fail
144  def testNumpyDim2BoolArrayToRepeated_RaisesTypeError(self):
145    with self.assertRaises(TypeError):
146      message.repeated_bool[:] = np_11_bool_array
147    with self.assertRaises(TypeError):
148      message.repeated_bool[:] = np_22_bool_array
149
150  # Assigning any np bool to scalar field should pass
151  def testNumpyBoolScalarToScalar_IsValid(self):
152    message.optional_bool = np_bool_scalar
153
154  # Assigning any ndarray of bool to scalar field should fail
155  def testNumpyBoolArrayToScalar_RaisesTypeError(self):
156    with self.assertRaises(TypeError):
157      message.optional_bool = np_1_bool_array
158    with self.assertRaises(TypeError):
159      message.optional_bool = np_11_bool_array
160    with self.assertRaises(TypeError):
161      message.optional_bool = np_22_bool_array
162
163
164@testing_refleaks.TestCase
165class NumpyProtoIndexingTest(unittest.TestCase):
166
167  def testNumpyIntScalarIndexing_Passes(self):
168    data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2])
169    self.assertEqual(0, data.repeated_int64[np.int64(0)])
170
171  def testNumpyNegative1IntScalarIndexing_Passes(self):
172    data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2])
173    self.assertEqual(2, data.repeated_int64[np.int64(-1)])
174
175  def testNumpyFloatScalarIndexing_Fails(self):
176    data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2])
177    with self.assertRaises(TypeError):
178      _ = data.repeated_int64[np.float64(0.0)]
179
180  def testNumpyIntArrayIndexing_Fails(self):
181    data = unittest_pb2.TestAllTypes(repeated_int64=[0, 1, 2])
182    with self.assertRaises(TypeError):
183      _ = data.repeated_int64[np.array([0])]
184    with self.assertRaises(TypeError):
185      _ = data.repeated_int64[np.ndarray((1,), buffer=np.array([0]), dtype=int)]
186    with self.assertRaises(TypeError):
187      _ = data.repeated_int64[np.ndarray((1, 1),
188                                         buffer=np.array([0]),
189                                         dtype=int)]
190
191if __name__ == '__main__':
192  unittest.main()
193