|
| 1 | +% verify_savemat.m |
| 2 | +% |
| 3 | +% MATLAB-side verification of files produced by mat73.savemat. |
| 4 | +% |
| 5 | +% Workflow: |
| 6 | +% 1. Run: python tests/create_savemat_testfiles.py |
| 7 | +% 2. In MATLAB, cd to the project root and run: tests/verify_savemat |
| 8 | +% |
| 9 | +% Each block prints "PASS" or throws an error on failure. |
| 10 | + |
| 11 | +clear; clc; |
| 12 | +load(fullfile(fileparts(mfilename('fullpath')), 'savemat_verify.mat')); |
| 13 | + |
| 14 | +passed = 0; |
| 15 | +failed = 0; |
| 16 | + |
| 17 | +function check(label, cond) |
| 18 | + if cond |
| 19 | + fprintf(' PASS %s\n', label); |
| 20 | + else |
| 21 | + fprintf(' FAIL %s\n', label); |
| 22 | + error('Assertion failed: %s', label); |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +fprintf('\n=== Python scalars ===\n'); |
| 27 | +check('scalar_int == 42', scalar_int == 42); |
| 28 | +check('scalar_int class double', isa(scalar_int, 'double')); |
| 29 | +check('scalar_float == 3.14', abs(scalar_float - 3.14) < 1e-10); |
| 30 | +check('scalar_neg == -1.5', scalar_neg == -1.5); |
| 31 | +check('scalar_bool_true == true', scalar_bool_true == true); |
| 32 | +check('scalar_bool_true class logical', isa(scalar_bool_true, 'logical')); |
| 33 | +check('scalar_bool_false == false', scalar_bool_false == false); |
| 34 | +check('scalar_bool_false class logical', isa(scalar_bool_false, 'logical')); |
| 35 | +check('scalar_str == ''hello''', strcmp(scalar_str, 'hello')); |
| 36 | +check('scalar_nan is NaN', isnan(scalar_nan)); |
| 37 | +check('scalar_inf is Inf', isinf(scalar_inf) && scalar_inf > 0); |
| 38 | +check('scalar_none is empty', isempty(scalar_none)); |
| 39 | + |
| 40 | +fprintf('\n=== NumPy scalars (dtype preservation) ===\n'); |
| 41 | +check('np_float64 value', abs(np_float64 - 1.23) < 1e-10); |
| 42 | +check('np_float64 class', isa(np_float64, 'double')); |
| 43 | +check('np_float32 value', abs(np_float32 - 1.5) < 1e-5); |
| 44 | +check('np_float32 class', isa(np_float32, 'single')); |
| 45 | +check('np_int8 value', np_int8 == 10); |
| 46 | +check('np_int8 class', isa(np_int8, 'int8')); |
| 47 | +check('np_int16 value', np_int16 == 1000); |
| 48 | +check('np_int16 class', isa(np_int16, 'int16')); |
| 49 | +check('np_int32 value', np_int32 == 99999); |
| 50 | +check('np_int32 class', isa(np_int32, 'int32')); |
| 51 | +check('np_int64 value', np_int64 == 1e9); |
| 52 | +check('np_int64 class', isa(np_int64, 'int64')); |
| 53 | +check('np_uint8 value', np_uint8 == 255); |
| 54 | +check('np_uint8 class', isa(np_uint8, 'uint8')); |
| 55 | +check('np_uint16 value', np_uint16 == 60000); |
| 56 | +check('np_uint16 class', isa(np_uint16, 'uint16')); |
| 57 | +check('np_uint32 value', np_uint32 == 1e6); |
| 58 | +check('np_uint32 class', isa(np_uint32, 'uint32')); |
| 59 | +check('np_uint64 value', np_uint64 == 1e12); |
| 60 | +check('np_uint64 class', isa(np_uint64, 'uint64')); |
| 61 | +check('np_bool value', np_bool == true); |
| 62 | +check('np_bool class', isa(np_bool, 'logical')); |
| 63 | + |
| 64 | +fprintf('\n=== NumPy arrays (dtype preservation) ===\n'); |
| 65 | +check('arr_float64 values', isequal(arr_float64, [1.0 2.0 3.0])); |
| 66 | +check('arr_float64 class', isa(arr_float64, 'double')); |
| 67 | +check('arr_float32 values', isequal(arr_float32, single([1.0 2.0 3.0]))); |
| 68 | +check('arr_float32 class', isa(arr_float32, 'single')); |
| 69 | +check('arr_int8 values', isequal(arr_int8, int8([-1 0 1]))); |
| 70 | +check('arr_int8 class', isa(arr_int8, 'int8')); |
| 71 | +check('arr_int16 values', isequal(arr_int16, int16([100 200 300]))); |
| 72 | +check('arr_int16 class', isa(arr_int16, 'int16')); |
| 73 | +check('arr_int32 values', isequal(arr_int32, int32([1000 2000 3000]))); |
| 74 | +check('arr_int32 class', isa(arr_int32, 'int32')); |
| 75 | +check('arr_int64 values', isequal(arr_int64, int64([1e9 2e9]))); |
| 76 | +check('arr_int64 class', isa(arr_int64, 'int64')); |
| 77 | +check('arr_uint8 values', isequal(arr_uint8, uint8([0 128 255]))); |
| 78 | +check('arr_uint8 class', isa(arr_uint8, 'uint8')); |
| 79 | +check('arr_uint16 values', isequal(arr_uint16, uint16([0 1000 65535]))); |
| 80 | +check('arr_uint16 class', isa(arr_uint16, 'uint16')); |
| 81 | +check('arr_uint32 values', isequal(arr_uint32, uint32([0 1e6]))); |
| 82 | +check('arr_uint32 class', isa(arr_uint32, 'uint32')); |
| 83 | +check('arr_uint64 values', isequal(arr_uint64, uint64([0 1e12]))); |
| 84 | +check('arr_uint64 class', isa(arr_uint64, 'uint64')); |
| 85 | +check('arr_bool values', isequal(arr_bool, logical([1 0 1]))); |
| 86 | +check('arr_bool class', isa(arr_bool, 'logical')); |
| 87 | +check('arr_nan(1) == 1', arr_nan(1) == 1.0); |
| 88 | +check('arr_nan(2) is NaN', isnan(arr_nan(2))); |
| 89 | +check('arr_nan(3) == 3', arr_nan(3) == 3.0); |
| 90 | +check('arr_complex values', isequal(arr_complex, [1+2i, 3+4i])); |
| 91 | +check('arr_complex class', isa(arr_complex, 'double')); |
| 92 | + |
| 93 | +fprintf('\n=== Array shapes ===\n'); |
| 94 | +check('arr_2d size', isequal(size(arr_2d), [2 3])); |
| 95 | +check('arr_2d values', isequal(arr_2d, [1 2 3; 4 5 6])); |
| 96 | +check('arr_3d size', isequal(size(arr_3d), [2 3 4])); |
| 97 | +check('arr_3d(1,1,1)', arr_3d(1,1,1) == 0); |
| 98 | +check('arr_3d(2,3,4)', arr_3d(2,3,4) == 23); |
| 99 | +check('arr_row size', isequal(size(arr_row), [1 3])); |
| 100 | +check('arr_row values', isequal(arr_row, [10 20 30])); |
| 101 | +check('arr_col size', isequal(size(arr_col), [3 1])); |
| 102 | +check('arr_col values', isequal(arr_col, [10; 20; 30])); |
| 103 | + |
| 104 | +fprintf('\n=== Python lists ===\n'); |
| 105 | +check('list_ints values', isequal(list_ints, [1 2 3])); |
| 106 | +check('list_ints class', isa(list_ints, 'double')); |
| 107 | +check('list_floats(2)', abs(list_floats(2) - 2.2) < 1e-10); |
| 108 | +check('list_nested size', isequal(size(list_nested), [2 2])); |
| 109 | +check('list_nested values', isequal(list_nested, [1 2; 3 4])); |
| 110 | +check('list_strs is cell', iscell(list_strs)); |
| 111 | +check('list_strs size', isequal(size(list_strs), [1 3])); |
| 112 | +check('list_strs{1}', strcmp(list_strs{1}, 'foo')); |
| 113 | +check('list_strs{2}', strcmp(list_strs{2}, 'bar')); |
| 114 | +check('list_strs{3}', strcmp(list_strs{3}, 'baz')); |
| 115 | + |
| 116 | +fprintf('\n=== Python dict (struct) ===\n'); |
| 117 | +check('mystruct is struct', isstruct(mystruct)); |
| 118 | +check('mystruct.double_field == 42',abs(mystruct.double_field - 42.0) < 1e-10); |
| 119 | +check('mystruct.str_field', strcmp(mystruct.str_field, 'world')); |
| 120 | +check('mystruct.arr_field values', isequal(mystruct.arr_field, [10 20 30])); |
| 121 | +check('mystruct.int_field == 7', mystruct.int_field == int32(7)); |
| 122 | +check('mystruct.int_field class', isa(mystruct.int_field, 'int32')); |
| 123 | + |
| 124 | +fprintf('\n=== Nested struct ===\n'); |
| 125 | +check('nested_struct is struct', isstruct(nested_struct)); |
| 126 | +check('nested_struct.outer_val == 1', nested_struct.outer_val == 1.0); |
| 127 | +check('nested_struct.inner is struct', isstruct(nested_struct.inner)); |
| 128 | +check('nested_struct.inner.inner_val', nested_struct.inner.inner_val == 2.0); |
| 129 | +check('nested_struct.inner.inner_str', strcmp(nested_struct.inner.inner_str, 'deep')); |
| 130 | + |
| 131 | +fprintf('\n=== All checks passed ===\n'); |
0 commit comments