1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
|
# Copyright (c) 2013-2016, Freja Nordsiek
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import copy
import os
import os.path
import math
import random
import collections
import numpy as np
import numpy.random
import hdf5storage
from functools import wraps
def raises(*exceptions):
valid = ' or '.join([e.__name__ for e in exceptions])
def decorate(func):
name = func.__name__
def newfunc(*arg, **kw):
try:
func(*arg, **kw)
except exceptions:
pass
except:
raise
else:
message = "%s() did not raise %s" % (name, valid)
raise AssertionError(message)
newfunc = wraps(func)(newfunc)
return newfunc
return decorate
from asserts import *
from make_randoms import *
random.seed()
class TestPythonMatlabFormat(object):
# Test for the ability to write python types to an HDF5 file that
# type information and matlab information are stored in, and then
# read it back and have it be the same.
def __init__(self):
self.filename = 'data.mat'
self.options = hdf5storage.Options()
# Need a list of the supported numeric dtypes to test, excluding
# those not supported by MATLAB. 'S' and 'U' dtype chars have to
# be used for the bare byte and unicode string dtypes since the
# dtype strings (but not chars) are not the same in Python 2 and
# 3.
self.dtypes = ['bool', 'uint8', 'uint16', 'uint32', 'uint64',
'int8', 'int16', 'int32', 'int64',
'float32', 'float64', 'complex64', 'complex128',
'S', 'U']
def write_readback(self, data, name, options, read_options=None):
# Write the data to the proper file with the given name, read it
# back, and return the result. The file needs to be deleted
# before and after to keep junk from building up. Different
# options can be used for reading the data back.
if os.path.exists(self.filename):
os.remove(self.filename)
try:
hdf5storage.write(data, path=name, filename=self.filename,
options=options)
out = hdf5storage.read(path=name, filename=self.filename,
options=read_options)
except:
raise
finally:
if os.path.exists(self.filename):
os.remove(self.filename)
return out
def assert_equal(self, a, b):
assert_equal(a, b)
def check_numpy_scalar(self, dtype):
# Makes a random numpy scalar of the given type, writes it and
# reads it back, and then compares it.
data = random_numpy_scalar(dtype)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_array(self, dtype, dimensions):
# Makes a random numpy array of the given type, writes it and
# reads it back, and then compares it.
shape = random_numpy_shape(dimensions,
max_array_axis_length)
data = random_numpy(shape, dtype)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_empty(self, dtype):
# Makes an empty numpy array of the given type, writes it and
# reads it back, and then compares it.
data = np.array([], dtype)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_stringlike_empty(self, dtype, num_chars):
# Makes an empty stringlike numpy array of the given type and
# size, writes it and reads it back, and then compares it.
data = np.array([], dtype + str(num_chars))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_structured_array(self, dimensions):
# Makes a random structured ndarray of the given type, writes it
# and reads it back, and then compares it.
shape = random_numpy_shape(dimensions, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_structured_array_empty(self, dimensions):
# Makes a random structured ndarray of the given type, writes it
# and reads it back, and then compares it.
shape = random_numpy_shape(dimensions, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape, (1, 0))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_structured_array_field_special_char(self, ch):
# Makes a random 1d structured ndarray with the character
# in one field, writes it and reads it back, and then compares
# it.
field_names = [random_str_ascii(max_dict_key_length)
for i in range(2)]
field_names[1] = field_names[1][0] + ch + field_names[1][1:]
if sys.hexversion < 0x03000000:
for i in range(len(field_names)):
field_names[i] = field_names[i].encode('UTF-8')
shape = random_numpy_shape(1, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape, names=field_names)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_matrix(self, dtype):
# Makes a random numpy array of the given type, converts it to
# a matrix, writes it and reads it back, and then compares it.
shape = random_numpy_shape(2, max_array_axis_length)
data = np.matrix(random_numpy(shape, dtype))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_recarray(self, dimensions):
# Makes a random structured ndarray of the given type, converts
# it to a recarray, writes it and reads it back, and then
# compares it.
shape = random_numpy_shape(dimensions, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape).view(np.recarray).copy()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_recarray_empty(self, dimensions):
# Makes a random structured ndarray of the given type, converts
# it to a recarray, writes it and reads it back, and then
# compares it.
shape = random_numpy_shape(dimensions, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape, (1, 0)).view(np.recarray).copy()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_recarray_field_special_char(self, ch):
# Makes a random 1d structured ndarray with the character
# in one field, converts it to a recarray, writes it and reads
# it back, and then compares it.
field_names = [random_str_ascii(max_dict_key_length)
for i in range(2)]
field_names[1] = field_names[1][0] + ch + field_names[1][1:]
if sys.hexversion < 0x03000000:
for i in range(len(field_names)):
field_names[i] = field_names[i].encode('UTF-8')
shape = random_numpy_shape(1, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape, names=field_names).view(np.recarray).copy()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_chararray(self, dimensions):
# Makes a random numpy array of bytes, converts it to a
# chararray, writes it and reads it back, and then compares it.
shape = random_numpy_shape(dimensions,
max_array_axis_length)
data = random_numpy(shape, 'S').view(np.chararray).copy()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_chararray_empty(self, num_chars):
# Makes an empty numpy array of bytes of the given number of
# characters, converts it to a chararray, writes it and reads it
# back, and then compares it.
data = np.array([], 'S' + str(num_chars)).view(np.chararray).copy()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_sized_dtype_nested_0(self, zero_shaped):
dtypes = ('uint8', 'uint16', 'uint32', 'uint64',
'int8', 'int16', 'int32', 'int64',
'float32', 'float64',
'complex64', 'complex128')
for i in range(10):
dt = (random.choice(dtypes), (2, 2 * zero_shaped))
data = np.zeros((2, ), dtype=dt)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_sized_dtype_nested_1(self, zero_shaped):
dtypes = ('uint8', 'uint16', 'uint32', 'uint64',
'int8', 'int16', 'int32', 'int64',
'float32', 'float64',
'complex64', 'complex128')
for i in range(10):
dt = [('a', random.choice(dtypes), (1, 2)),
('b', random.choice(dtypes), (1, 1, 4 * zero_shaped)),
('c',
[('a', random.choice(dtypes)),
('b', random.choice(dtypes), (1, 2))])]
data = np.zeros((random.randrange(1, 4), ), dtype=dt)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_sized_dtype_nested_2(self, zero_shaped):
dtypes = ('uint8', 'uint16', 'uint32', 'uint64',
'int8', 'int16', 'int32', 'int64',
'float32', 'float64',
'complex64', 'complex128')
for i in range(10):
dt = [('a', random.choice(dtypes), (1, 3)),
('b',
[('a', random.choice(dtypes), (2, )),
('b', random.choice(dtypes), (1, 2, 1))]),
('c',
[('a', random.choice(dtypes),
(3 * zero_shaped, 1)),
('b', random.choice(dtypes), (2, ))], (2, 1))]
data = np.zeros((2, ), dtype=dt)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_numpy_sized_dtype_nested_3(self, zero_shaped):
dtypes = ('uint8', 'uint16', 'uint32', 'uint64',
'int8', 'int16', 'int32', 'int64',
'float32', 'float64',
'complex64', 'complex128')
for i in range(10):
dt = [('a', random.choice(dtypes), (3, 2)),
('b',
[('a',
[('a', random.choice(dtypes))],
(2, 2)),
('b', random.choice(dtypes), (1, 2))]),
('c',
[('a',
[('a', random.choice(dtypes),
(2, 1, zero_shaped * 2))]),
('b', random.choice(dtypes))])]
data = np.zeros((1, ), dtype=dt)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def check_python_collection(self, tp, same_dims):
# Makes a random collection of the specified type, writes it and
# reads it back, and then compares it.
if tp in (set, frozenset):
data = tp(random_list(max_list_length,
python_or_numpy='python'))
else:
if same_dims == 'same-dims':
shape = random_numpy_shape(random.randrange(2, 4),
random.randrange(1, 4))
dtypes = ('uint8', 'uint16', 'uint32', 'uint64',
'int8', 'int16', 'int32', 'int64',
'float32', 'float64',
'complex64', 'complex128')
data = tp([random_numpy(shape, random.choice(dtypes),
allow_nan=True)
for i in range(random.randrange(2, 7))])
elif same_dims == 'diff-dims':
data = tp(random_list(max_list_length,
python_or_numpy='numpy'))
else:
raise ValueError('invalid value of same_dims')
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_None(self):
data = None
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_bool_True(self):
data = True
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_bool_False(self):
data = False
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_int_needs_32_bits(self):
data = random_int()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_int_needs_64_bits(self):
data = (2**32) * random_int()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
# Only relevant in Python 2.x.
def test_long_needs_32_bits(self):
if sys.hexversion < 0x03000000:
data = long(random_int())
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
# Only relevant in Python 2.x.
def test_long_needs_64_bits(self):
if sys.hexversion < 0x03000000:
data = long(2)**32 * long(random_int())
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_int_or_long_too_big(self):
if sys.hexversion >= 0x03000000:
data = 2**64 * random_int()
else:
data = long(2)**64 * long(random_int())
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_float(self):
data = random_float()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_float_inf(self):
data = float(np.inf)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_float_ninf(self):
data = float(-np.inf)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_float_nan(self):
data = float(np.nan)
out = self.write_readback(data, random_name(),
self.options)
assert math.isnan(out)
def test_complex(self):
data = random_float() + 1j*random_float()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_complex_real_nan(self):
data = complex(np.nan, random_float())
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_complex_imaginary_nan(self):
data = complex(random_float(), np.nan)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_str_ascii(self):
data = random_str_ascii(random.randint(1,
max_string_length))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_str_ascii_encoded_utf8(self):
ltrs = string.ascii_letters + string.digits
data = 'a'
if sys.hexversion < 0x03000000:
data = unicode(data)
ltrs = unicode(ltrs)
while all([(c in ltrs) for c in data]):
data = random_str_some_unicode(random.randint(1, \
max_string_length))
data = data.encode('utf-8')
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_str_unicode(self):
data = random_str_some_unicode(random.randint(1,
max_string_length))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_str_empty(self):
data = ''
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_bytes(self):
data = random_bytes(random.randint(1,
max_string_length))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_bytes_empty(self):
data = b''
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_bytearray(self):
data = bytearray(random_bytes(random.randint(1,
max_string_length)))
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_bytearray_empty(self):
data = bytearray(b'')
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
def test_numpy_scalar(self):
for dt in self.dtypes:
yield self.check_numpy_scalar, dt
def test_numpy_array_1d(self):
dts = copy.deepcopy(self.dtypes)
dts.append('object')
for dt in dts:
yield self.check_numpy_array, dt, 1
def test_numpy_array_2d(self):
dts = copy.deepcopy(self.dtypes)
dts.append('object')
for dt in dts:
yield self.check_numpy_array, dt, 2
def test_numpy_array_3d(self):
dts = copy.deepcopy(self.dtypes)
dts.append('object')
for dt in dts:
yield self.check_numpy_array, dt, 3
def test_numpy_matrix(self):
dts = copy.deepcopy(self.dtypes)
dts.append('object')
for dt in dts:
yield self.check_numpy_matrix, dt
def test_numpy_empty(self):
for dt in self.dtypes:
yield self.check_numpy_empty, dt
def test_numpy_stringlike_empty(self):
dts = ['S', 'U']
for dt in dts:
for n in range(1,10):
yield self.check_numpy_stringlike_empty, dt, n
def test_numpy_structured_array(self):
for i in range(1, 4):
yield self.check_numpy_structured_array, i
def test_numpy_structured_array_empty(self):
for i in range(1, 4):
yield self.check_numpy_structured_array_empty, i
def test_numpy_structured_array_unicode_fields(self):
# Makes a random 1d structured ndarray with non-ascii characters
# in its fields, writes it and reads it back, and then compares
# it.
shape = random_numpy_shape(1, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape, \
nonascii_fields=True).view(np.recarray).copy()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_numpy_structured_array_field_null_character(self):
self.check_numpy_structured_array_field_special_char('\x00')
@raises(NotImplementedError)
def test_numpy_structured_array_field_forward_slash(self):
self.check_numpy_structured_array_field_special_char('/')
def test_numpy_recarray(self):
for i in range(1, 4):
yield self.check_numpy_recarray, i
def test_numpy_recarray_empty(self):
for i in range(1, 4):
yield self.check_numpy_recarray_empty, i
def test_numpy_recarray_unicode_fields(self):
# Makes a random 1d structured ndarray with non-ascii characters
# in its fields, converts it to a recarray, writes it and reads
# it back, and then compares it.
shape = random_numpy_shape(1, \
max_structured_ndarray_axis_length)
data = random_structured_numpy_array(shape,
nonascii_fields=True)
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_numpy_recarray_field_null_character(self):
self.check_numpy_recarray_field_special_char('\x00')
@raises(NotImplementedError)
def test_numpy_recarray_field_forward_slash(self):
self.check_numpy_recarray_field_special_char('/')
def test_numpy_chararray(self):
dims = range(1, 4)
for dim in dims:
yield self.check_numpy_chararray, dim
def test_numpy_chararray_empty(self):
for n in range(1, 10):
yield self.check_numpy_chararray_empty, n
def test_numpy_sized_dtype_nested_0(self):
for zero_shaped in (False, True):
yield self.check_numpy_sized_dtype_nested_0, zero_shaped
def test_numpy_sized_dtype_nested_1(self):
for zero_shaped in (False, True):
yield self.check_numpy_sized_dtype_nested_1, zero_shaped
def test_numpy_sized_dtype_nested_2(self):
for zero_shaped in (False, True):
yield self.check_numpy_sized_dtype_nested_2, zero_shaped
def test_numpy_sized_dtype_nested_3(self):
for zero_shaped in (False, True):
yield self.check_numpy_sized_dtype_nested_3, zero_shaped
def test_python_collection(self):
for tp in (list, tuple, set, frozenset, collections.deque):
yield self.check_python_collection, tp, 'same-dims'
yield self.check_python_collection, tp, 'diff-dims'
def test_dict(self):
data = random_dict()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_dict_bytes_key(self):
data = random_dict()
key = random_bytes(max_dict_key_length)
data[key] = random_int()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_dict_key_null_character(self):
data = random_dict()
if sys.hexversion >= 0x03000000:
ch = '\x00'
else:
ch = unicode('\x00')
key = ch.join([random_str_ascii(max_dict_key_length)
for i in range(2)])
data[key] = random_int()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
@raises(NotImplementedError)
def test_dict_key_forward_slash(self):
data = random_dict()
if sys.hexversion >= 0x03000000:
ch = '/'
else:
ch = unicode('/')
key = ch.join([random_str_ascii(max_dict_key_length)
for i in range(2)])
data[key] = random_int()
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
class TestPythonFormat(TestPythonMatlabFormat):
def __init__(self):
# The parent does most of the setup. All that has to be changed
# is turning MATLAB compatibility off and changing the file
# name.
TestPythonMatlabFormat.__init__(self)
self.options = hdf5storage.Options(matlab_compatible=False)
self.filename = 'data.h5'
# Won't throw an exception unlike the parent.
def test_str_ascii_encoded_utf8(self):
ltrs = string.ascii_letters + string.digits
data = 'a'
if sys.hexversion < 0x03000000:
data = unicode(data)
ltrs = unicode(ltrs)
while all([(c in ltrs) for c in data]):
data = random_str_some_unicode(random.randint(1, \
max_string_length))
data = data.encode('utf-8')
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
# Won't throw an exception unlike the parent.
def test_numpy_structured_array_field_forward_slash(self):
self.check_numpy_structured_array_field_special_char('/')
# Won't throw an exception unlike the parent.
def test_numpy_recarray_field_forward_slash(self):
self.check_numpy_recarray_field_special_char('/')
class TestNoneFormat(TestPythonMatlabFormat):
def __init__(self):
# The parent does most of the setup. All that has to be changed
# is turning off the storage of type information as well as
# MATLAB compatibility.
TestPythonMatlabFormat.__init__(self)
self.options = hdf5storage.Options(store_python_metadata=False,
matlab_compatible=False)
# Add in float16 to the set of types tested.
self.dtypes.append('float16')
# Won't throw an exception unlike the parent.
def test_str_ascii_encoded_utf8(self):
ltrs = string.ascii_letters + string.digits
data = 'a'
if sys.hexversion < 0x03000000:
data = unicode(data)
ltrs = unicode(ltrs)
while all([(c in ltrs) for c in data]):
data = random_str_some_unicode(random.randint(1, \
max_string_length))
data = data.encode('utf-8')
out = self.write_readback(data, random_name(),
self.options)
self.assert_equal(out, data)
# Won't throw an exception unlike the parent.
def test_numpy_structured_array_field_forward_slash(self):
self.check_numpy_structured_array_field_special_char('/')
# Won't throw an exception unlike the parent.
def test_numpy_recarray_field_forward_slash(self):
self.check_numpy_recarray_field_special_char('/')
def assert_equal(self, a, b):
assert_equal_none_format(a, b)
class TestMatlabFormat(TestPythonMatlabFormat):
def __init__(self):
# The parent does most of the setup. All that has to be changed
# is turning on the matlab compatibility, and changing the
# filename.
TestPythonMatlabFormat.__init__(self)
self.options = hdf5storage.Options(store_python_metadata=False,
matlab_compatible=True)
self.filename = 'data.mat'
def assert_equal(self, a, b):
assert_equal_matlab_format(a, b)
|