3232 "string and so lets test it and prove that shall we?"
3333
3434# The numpy dtype of all arrays of strings
35- DTYPE_STRING = "S40"
35+ NUMPY_DTYPE_STRING = "S40"
3636
3737
3838def record_func_names (fixture_value ):
@@ -194,15 +194,12 @@ def record_values_names(fixture_value):
194194 ),
195195 numpy .ndarray ,
196196 ),
197-
198- # TODO: Unicode versions of below tests?
199-
200197 (
201198 "wIn_byte_string_array" ,
202199 builder .WaveformIn ,
203200 [b"AB123" , b"CD456" , b"EF789" ],
204201 numpy .array (
205- ["AB123" , "CD456" , "EF789" ], dtype = DTYPE_STRING
202+ ["AB123" , "CD456" , "EF789" ], dtype = NUMPY_DTYPE_STRING
206203 ),
207204 numpy .ndarray ,
208205 ),
@@ -211,7 +208,35 @@ def record_values_names(fixture_value):
211208 builder .WaveformOut ,
212209 [b"12AB" , b"34CD" , b"56EF" ],
213210 numpy .array (
214- ["12AB" , "34CD" , "56EF" ], dtype = DTYPE_STRING
211+ ["12AB" , "34CD" , "56EF" ], dtype = NUMPY_DTYPE_STRING
212+ ),
213+ numpy .ndarray ,
214+ ),
215+ (
216+ "wIn_unicode_string_array" ,
217+ builder .WaveformIn ,
218+ ["12€½" , "34¾²" , "56¹³" ],
219+ numpy .array (
220+ [
221+ b'12\xe2 \x82 \xac \xc2 \xbd ' ,
222+ b'34\xc2 \xbe \xc2 \xb2 ' ,
223+ b'56\xc2 \xb9 \xc2 \xb3 '
224+ ],
225+ dtype = NUMPY_DTYPE_STRING
226+ ),
227+ numpy .ndarray ,
228+ ),
229+ (
230+ "wOut_unicode_string_array" ,
231+ builder .WaveformOut ,
232+ ["12€½" , "34¾²" , "56¹³" ],
233+ numpy .array (
234+ [
235+ b'12\xe2 \x82 \xac \xc2 \xbd ' ,
236+ b'34\xc2 \xbe \xc2 \xb2 ' ,
237+ b'56\xc2 \xb9 \xc2 \xb3 '
238+ ],
239+ dtype = NUMPY_DTYPE_STRING
215240 ),
216241 numpy .ndarray ,
217242 ),
@@ -220,7 +245,7 @@ def record_values_names(fixture_value):
220245 builder .WaveformIn ,
221246 ["123abc" , "456def" , "7890ghi" ],
222247 numpy .array (
223- ["123abc" , "456def" , "7890ghi" ], dtype = DTYPE_STRING
248+ ["123abc" , "456def" , "7890ghi" ], dtype = NUMPY_DTYPE_STRING
224249 ),
225250 numpy .ndarray ,
226251 ),
@@ -229,7 +254,7 @@ def record_values_names(fixture_value):
229254 builder .WaveformOut ,
230255 ["123abc" , "456def" , "7890ghi" ],
231256 numpy .array (
232- ["123abc" , "456def" , "7890ghi" ], dtype = DTYPE_STRING
257+ ["123abc" , "456def" , "7890ghi" ], dtype = NUMPY_DTYPE_STRING
233258 ),
234259 numpy .ndarray ,
235260 ),
@@ -539,10 +564,19 @@ def is_valid(configuration):
539564 "scalar. Therefore we skip this check." )
540565 continue
541566
567+ # caget on a waveform of strings will return unicode. Have to
568+ # convert it manually to binary.
542569 if isinstance (rec_val , numpy .ndarray ) and len (rec_val ) > 1 \
543570 and rec_val .dtype .char in ["S" , "U" ]:
571+ result = numpy .empty (len (rec_val ), NUMPY_DTYPE_STRING )
572+ for n , s in enumerate (rec_val ):
573+ if isinstance (s , str ):
574+ result [n ] = s .encode ('UTF-8' , errors = 'ignore' )
575+ else :
576+ result [n ] = s
577+ rec_val = result
544578 # caget won't retrieve the full length 40 buffer
545- rec_val = rec_val .astype (DTYPE_STRING )
579+ rec_val = rec_val .astype (NUMPY_DTYPE_STRING )
546580
547581 record_value_asserts (
548582 creation_func , rec_val , expected_value , expected_type
@@ -958,7 +992,46 @@ def test_waveform_rejects_overlong_values(self):
958992 w_in = builder .WaveformIn ("W_IN" , [1 , 2 , 3 ])
959993 w_out = builder .WaveformOut ("W_OUT" , [1 , 2 , 3 ])
960994
995+ w_in_str = builder .WaveformIn ("W_IN_STR" , ["ABC" , "DEF" ])
996+ w_out_str = builder .WaveformOut ("W_OUT_STR" , ["ABC" , "DEF" ])
997+
961998 with pytest .raises (AssertionError ):
962999 w_in .set ([1 , 2 , 3 , 4 ])
9631000 with pytest .raises (AssertionError ):
9641001 w_out .set ([1 , 2 , 3 , 4 ])
1002+ with pytest .raises (AssertionError ):
1003+ w_in_str .set (["ABC" , "DEF" , "GHI" ])
1004+ with pytest .raises (AssertionError ):
1005+ w_out_str .set (["ABC" , "DEF" , "GHI" ])
1006+
1007+ def test_waveform_rejects_late_strings (self ):
1008+ """Test that a waveform won't allow a list of strings to be assigned
1009+ if no list was given in initial waveform construction"""
1010+ w_in = builder .WaveformIn ("W_IN" , length = 10 )
1011+ w_out = builder .WaveformOut ("W_OUT" , length = 10 )
1012+
1013+ with pytest .raises (ValueError ):
1014+ w_in .set (["ABC" , "DEF" ])
1015+ with pytest .raises (ValueError ):
1016+ w_out .set (["ABC" , "DEF" ])
1017+
1018+ def test_waveform_rejects_long_array_of_strings (self ):
1019+ """Test that a waveform of strings won't accept too long strings"""
1020+ w_in = builder .WaveformIn (
1021+ "W_IN" ,
1022+ initial_value = ["123abc" , "456def" , "7890ghi" ]
1023+ )
1024+ w_out = builder .WaveformIn (
1025+ "W_OUT" ,
1026+ initial_value = ["123abc" , "456def" , "7890ghi" ]
1027+ )
1028+
1029+ with pytest .raises (AssertionError ):
1030+ w_in .set (["1" , "2" , "3" , "4" ])
1031+ with pytest .raises (AssertionError ):
1032+ w_out .set (["1" , "2" , "3" , "4" ])
1033+
1034+ with pytest .raises (ValueError ):
1035+ w_in .set ([VERY_LONG_STRING ])
1036+ with pytest .raises (ValueError ):
1037+ w_out .set ([VERY_LONG_STRING ])
0 commit comments