@@ -72,7 +72,8 @@ def get_input(self, n):
72
72
73
73
class CharColumn (Column ):
74
74
def get_input (self , n ):
75
- return np .zeros (n , dtype = np .int8 )
75
+ rng = np .random .RandomState (42 )
76
+ return rng .randint (low = 0 , high = 127 , size = n , dtype = np .int8 )
76
77
77
78
78
79
class DoubleColumn (Column ):
@@ -87,13 +88,32 @@ class CommonTestsMixin:
87
88
"""
88
89
89
90
def make_input_data (self , num_rows ):
91
+ rng = np .random .RandomState (42 )
90
92
input_data = {col .name : col .get_input (num_rows ) for col in self .columns }
91
93
for list_col , offset_col in self .ragged_list_columns :
92
- value = list_col .get_input (num_rows )
93
- input_data [list_col .name ] = value
94
- input_data [offset_col .name ] = np .arange (num_rows + 1 , dtype = np .uint32 )
94
+ lengths = rng .randint (low = 0 , high = 10 , size = num_rows )
95
+ input_data [list_col .name ] = list_col .get_input (sum (lengths ))
96
+ input_data [offset_col .name ] = np .zeros (num_rows + 1 , dtype = np .uint32 )
97
+ input_data [offset_col .name ][1 :] = np .cumsum (lengths , dtype = np .uint32 )
95
98
return input_data
96
99
100
+ def make_transposed_input_data (self , num_rows ):
101
+ cols = self .make_input_data (num_rows )
102
+ return [
103
+ {
104
+ col : data [j ]
105
+ if len (data ) == num_rows
106
+ else (
107
+ bytes (data [cols [f"{ col } _offset" ][j ] : cols [f"{ col } _offset" ][j + 1 ]])
108
+ if "metadata" in col
109
+ else data [cols [f"{ col } _offset" ][j ] : cols [f"{ col } _offset" ][j + 1 ]]
110
+ )
111
+ for col , data in cols .items ()
112
+ if "offset" not in col
113
+ }
114
+ for j in range (num_rows )
115
+ ]
116
+
97
117
def test_max_rows_increment (self ):
98
118
for bad_value in [- 1 , - (2 ** 10 )]:
99
119
with pytest .raises (ValueError ):
@@ -279,15 +299,17 @@ def test_set_column_attributes_data(self):
279
299
setattr (table , list_col .name , list_data )
280
300
assert np .array_equal (getattr (table , list_col .name ), list_data )
281
301
list_value = getattr (table [0 ], list_col .name )
282
- assert len (list_value ) == 1
302
+ assert len (list_value ) == input_data [ offset_col . name ][ 1 ]
283
303
284
304
# Reset the offsets so that all the full array is associated with the
285
305
# first element.
286
- offset_data = np .zeros (num_rows + 1 , dtype = np .uint32 ) + num_rows
306
+ offset_data = np .zeros (num_rows + 1 , dtype = np .uint32 ) + len (
307
+ input_data [list_col .name ]
308
+ )
287
309
offset_data [0 ] = 0
288
310
setattr (table , offset_col .name , offset_data )
289
311
list_value = getattr (table [0 ], list_col .name )
290
- assert len (list_value ) == num_rows
312
+ assert len (list_value ) == len ( input_data [ list_col . name ])
291
313
292
314
del input_data [list_col .name ]
293
315
del input_data [offset_col .name ]
@@ -338,17 +360,11 @@ def test_defaults(self):
338
360
339
361
def test_add_row_data (self ):
340
362
for num_rows in [0 , 10 , 100 ]:
341
- input_data = {col .name : col .get_input (num_rows ) for col in self .columns }
342
363
table = self .table_class ()
343
- for j in range (num_rows ):
344
- kwargs = {col : data [j ] for col , data in input_data .items ()}
345
- for col in self .string_colnames :
346
- kwargs [col ] = "x"
347
- for col in self .binary_colnames :
348
- kwargs [col ] = b"x"
349
- k = table .add_row (** kwargs )
364
+ for j , row in enumerate (self .make_transposed_input_data (num_rows )):
365
+ k = table .add_row (** row )
350
366
assert k == j
351
- for colname , input_array in input_data .items ():
367
+ for colname , input_array in self . make_input_data ( num_rows ) .items ():
352
368
output_array = getattr (table , colname )
353
369
assert input_array .shape == output_array .shape
354
370
assert np .all (input_array == output_array )
@@ -573,6 +589,9 @@ def test_equality(self):
573
589
value = list_col .get_input (num_rows )
574
590
input_data_copy = dict (input_data )
575
591
input_data_copy [list_col .name ] = value + 1
592
+ input_data_copy [offset_col .name ] = np .arange (
593
+ num_rows + 1 , dtype = np .uint32
594
+ )
576
595
t2 .set_columns (** input_data_copy )
577
596
assert t1 != t2
578
597
assert t1 [0 ] != t2 [0 ]
@@ -607,35 +626,36 @@ def test_bad_offsets(self):
607
626
t .set_columns (** input_data )
608
627
609
628
for _list_col , offset_col in self .ragged_list_columns :
629
+ original_offset = np .copy (input_data [offset_col .name ])
610
630
input_data [offset_col .name ][0 ] = - 1
611
631
with pytest .raises (ValueError ):
612
632
t .set_columns (** input_data )
613
- input_data [offset_col .name ] = np .arange ( num_rows + 1 , dtype = np . uint32 )
633
+ input_data [offset_col .name ] = np .copy ( original_offset )
614
634
t .set_columns (** input_data )
615
635
input_data [offset_col .name ][- 1 ] = 0
616
636
with pytest .raises (ValueError ):
617
637
t .set_columns (** input_data )
618
- input_data [offset_col .name ] = np .arange ( num_rows + 1 , dtype = np . uint32 )
638
+ input_data [offset_col .name ] = np .copy ( original_offset )
619
639
t .set_columns (** input_data )
620
640
input_data [offset_col .name ][num_rows // 2 ] = 2 ** 31
621
641
with pytest .raises (ValueError ):
622
642
t .set_columns (** input_data )
623
- input_data [offset_col .name ] = np .arange ( num_rows + 1 , dtype = np . uint32 )
643
+ input_data [offset_col .name ] = np .copy ( original_offset )
624
644
625
645
input_data [offset_col .name ][0 ] = - 1
626
646
with pytest .raises (ValueError ):
627
647
t .append_columns (** input_data )
628
- input_data [offset_col .name ] = np .arange ( num_rows + 1 , dtype = np . uint32 )
648
+ input_data [offset_col .name ] = np .copy ( original_offset )
629
649
t .append_columns (** input_data )
630
650
input_data [offset_col .name ][- 1 ] = 0
631
651
with pytest .raises (ValueError ):
632
652
t .append_columns (** input_data )
633
- input_data [offset_col .name ] = np .arange ( num_rows + 1 , dtype = np . uint32 )
653
+ input_data [offset_col .name ] = np .copy ( original_offset )
634
654
t .append_columns (** input_data )
635
655
input_data [offset_col .name ][num_rows // 2 ] = 2 ** 31
636
656
with pytest .raises (ValueError ):
637
657
t .append_columns (** input_data )
638
- input_data [offset_col .name ] = np .arange ( num_rows + 1 , dtype = np . uint32 )
658
+ input_data [offset_col .name ] = np .copy ( original_offset )
639
659
640
660
641
661
class MetadataTestsMixin :
0 commit comments