@@ -82,6 +82,27 @@ def testToTuple(self):
8282 out = toTuple (1 , data3d ) # treat input a 1d array of compound type of compound types
8383 self .assertEqual ([((0 , 0.0 ), (1 , 0.1 )), ((2 , 0.2 ), (3 , 0.3 ))], out )
8484
85+ def testToTupleStrData (self ):
86+ data = "a string!"
87+ out = toTuple (0 , data )
88+ self .assertEqual (data , out )
89+
90+ data = ["a string!" ]
91+ out = toTuple (1 , data )
92+ self .assertEqual (data , out )
93+
94+ data = ["a string2" ]
95+ out = toTuple (1 , data )
96+ self .assertEqual (data , out )
97+
98+ data = [["partA" , "partB" , "partC" ],]
99+ out = toTuple (1 , data )
100+ self .assertEqual ([("partA" , "partB" , "partC" ), ], out )
101+
102+ data = [[[4 , 8 , 12 ], "four" ], [[5 , 10 , 15 ], "five" ]]
103+ out = toTuple (1 , data )
104+ self .assertEqual ([((4 , 8 , 12 ), 'four' ), ((5 , 10 , 15 ), 'five' )], out )
105+
85106 def testGetNumElements (self ):
86107 shape = (4 ,)
87108 nelements = getNumElements (shape )
@@ -98,7 +119,6 @@ def testGetNumElements(self):
98119 def testJsonToArray (self ):
99120
100121 # simple integer
101-
102122 dt = np .dtype ("i4" )
103123 shape = [4 , ]
104124 data = [0 , 2 , 4 , 6 ]
@@ -151,6 +171,14 @@ def testJsonToArray(self):
151171 val = out [0 ]
152172 self .assertEqual (val , data )
153173
174+ # VLEN multi element
175+ shape = [5 , ]
176+ data = ["parting" , "is" , "such" , "sweet" , "sorrow" ]
177+ out = jsonToArray (shape , dt , data )
178+ self .assertTrue (isinstance (out , np .ndarray ))
179+ self .assertEqual (out .shape , (5 , ))
180+ self .assertEqual (out [4 ], 'sorrow' )
181+
154182 # VLEN ascii
155183 dt = special_dtype (vlen = bytes )
156184 data = [b"one" , b"two" , b"three" , b"four" , b"five" ]
@@ -167,22 +195,6 @@ def testJsonToArray(self):
167195 self .assertEqual (out [2 ], b"three" )
168196 self .assertEqual (out [3 ], b"four" )
169197
170- # VLEN str
171- dt = special_dtype (vlen = str )
172- data = [
173- [b"part 1 - section A" , b"part 1 - section B" ],
174- [b"part 2 - section A" , b"part 2 - section B" ],
175- ]
176- shape = [2 ,]
177- out = jsonToArray (shape , dt , data )
178- self .assertTrue (isinstance (out , np .ndarray ))
179- self .assertTrue ("vlen" in out .dtype .metadata )
180- self .assertEqual (out .dtype .metadata ["vlen" ], str )
181- self .assertEqual (out .dtype .kind , "O" )
182- self .assertEqual (out .shape , (2 ,))
183- self .assertEqual (out [0 ], tuple (data [0 ]))
184- self .assertEqual (out [1 ], tuple (data [1 ]))
185-
186198 # VLEN unicode
187199 dt = special_dtype (vlen = bytes )
188200 data = ["one" , "two" , "three" , "four" , "five" ]
@@ -207,6 +219,12 @@ def testJsonToArray(self):
207219 self .assertTrue (isinstance (out , np .ndarray ))
208220 self .assertEqual (out [()], data )
209221
222+ data = ["I'm an UTF-8 null terminated string" ,]
223+ shape = [1 ,]
224+ out = jsonToArray (shape , dt , data )
225+ self .assertTrue (isinstance (out , np .ndarray ))
226+ self .assertEqual (out [0 ], data [0 ])
227+
210228 dt = np .dtype ("S12" )
211229 data = "eight: \u516b "
212230 out = jsonToArray (shape , dt , data )
@@ -223,9 +241,16 @@ def testJsonToArray(self):
223241 dt = np .dtype ("S12" )
224242 data = "eight: \u516b "
225243 out = jsonToArray (shape , dt , data )
244+ self .assertTrue (isinstance (out , np .ndarray ))
226245 self .assertEqual (out [0 ], b'eight: \xe5 \x85 \xab ' )
227246
228247 # VLEN data
248+ shape = []
249+ dt = special_dtype (vlen = np .dtype ("S10" ))
250+ data = ["foo" , "bar" ]
251+ out = jsonToArray (shape , dt , data )
252+ self .assertTrue (isinstance (out , np .ndarray ))
253+
229254 dt = special_dtype (vlen = np .dtype ("int32" ))
230255 shape = [4 , ]
231256 data = [
@@ -321,7 +346,7 @@ def testJsonToArray(self):
321346 e1 = out [1 ].tolist ()
322347 self .assertEqual (e1 , (5 , b"five" ))
323348
324- data = [6 , "six" ]
349+ data = [[ 6 , "six" ], ]
325350 shape = [1 ,]
326351 out = jsonToArray (shape , dt , data )
327352 self .assertTrue (isinstance (out , np .ndarray ))
@@ -352,7 +377,7 @@ def testJsonToArray(self):
352377 self .assertEqual (e0 , (4 , "four" ))
353378
354379 shape = [1 , ]
355- data = [6 , "six" ]
380+ data = [[ 6 , "six" ], ]
356381 out = jsonToArray (shape , dt , data )
357382 self .assertTrue (isinstance (out , np .ndarray ))
358383 self .assertEqual (out .shape , (1 ,))
0 commit comments