-
Notifications
You must be signed in to change notification settings - Fork 11
Add test for precision conversion with decl_hdf5
#672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6c022f9
4e88161
b342921
28e3989
3c39885
dfa8769
94997ce
b980a9d
bcdccf2
2774595
9e51956
071dff9
6c5ceaa
1c5c227
2c2c58b
ba0aed4
5150802
1dcb3c2
b01bb1b
bd034c8
3c5f4bc
106bfbf
eeab5c1
6a33909
7ac0058
ff9f88b
73cd0fc
8678b69
7a863c4
169bdad
f3d585a
6d26bee
c9817dd
f7794cc
fe561e8
b43a1c7
bf2cdc1
b842376
fc361bb
7b288e9
97edfe9
adc2e8d
f78960d
6d24ef8
21df9af
121fa7b
8ecb3a5
595124d
4db4f55
a83ab3b
450cdf8
0e1f68c
6ab160c
f12d601
20a8ab1
5e52643
dcfdf07
cec1507
ddedac5
4180b15
d493808
fd37e5a
5cdd046
8a01504
d372511
f60cb25
7102952
5dd30bb
717cbc6
4416895
54ac0f0
3dc95ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,147 @@ | ||||||||||||||||||||
| /******************************************************************************* | ||||||||||||||||||||
| * Copyright (C) 2026 Commissariat a l'energie atomique et aux energies alternatives (CEA) | ||||||||||||||||||||
| * All rights reserved. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||
| * modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||
| * * Redistributions of source code must retain the above copyright | ||||||||||||||||||||
| * notice, this list of conditions and the following disclaimer. | ||||||||||||||||||||
| * * 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. | ||||||||||||||||||||
| * * Neither the name of CEA nor the names of its contributors may be used to | ||||||||||||||||||||
| * endorse or promote products derived from this software without specific | ||||||||||||||||||||
| * prior written permission. | ||||||||||||||||||||
| * | ||||||||||||||||||||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||||||||||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||||||||||||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||||||||||||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||||||||||||||||
| * THE SOFTWARE. | ||||||||||||||||||||
| ******************************************************************************/ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <filesystem> | ||||||||||||||||||||
| #include <ranges> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <hdf5.h> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #include <pdi/testing.h> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| class DeclHdf5: public ::PDI::PdiTest | ||||||||||||||||||||
| {}; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /* Precision conversion with decl_hdf5 | ||||||||||||||||||||
| * data in double precision | ||||||||||||||||||||
| * file dataset in double, float, and int | ||||||||||||||||||||
| */ | ||||||||||||||||||||
| TEST_F(DeclHdf5, PrecisionConversion) | ||||||||||||||||||||
| { | ||||||||||||||||||||
| InitPdi(PC_parse_string(R"==( | ||||||||||||||||||||
| logging: trace | ||||||||||||||||||||
| metadata: | ||||||||||||||||||||
| N: int | ||||||||||||||||||||
| data: | ||||||||||||||||||||
| array: {type: array, size: [$N, $N], subtype: double} | ||||||||||||||||||||
| plugins: | ||||||||||||||||||||
| decl_hdf5: | ||||||||||||||||||||
| - file: d2d_test.h5 | ||||||||||||||||||||
| datasets: | ||||||||||||||||||||
| double_ds: {type: array, size: [$N, $N], subtype: double} | ||||||||||||||||||||
| write: | ||||||||||||||||||||
| array: | ||||||||||||||||||||
| dataset: double_ds | ||||||||||||||||||||
| - file: d2f_test.h5 | ||||||||||||||||||||
| datasets: | ||||||||||||||||||||
| float_ds: {type: array, size: [$N, $N], subtype: float} | ||||||||||||||||||||
| write: | ||||||||||||||||||||
| array: | ||||||||||||||||||||
| dataset: float_ds | ||||||||||||||||||||
| - file: d2i_test.h5 | ||||||||||||||||||||
| datasets: | ||||||||||||||||||||
| int_ds: {type: array, size: [$N, $N], subtype: int} | ||||||||||||||||||||
| write: | ||||||||||||||||||||
| array: | ||||||||||||||||||||
| dataset: int_ds | ||||||||||||||||||||
| )==")); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| EXPECT_FALSE(std::filesystem::exists("d2d_test.h5")); | ||||||||||||||||||||
| EXPECT_FALSE(std::filesystem::exists("d2f_test.h5")); | ||||||||||||||||||||
| EXPECT_FALSE(std::filesystem::exists("d2i_test.h5")); | ||||||||||||||||||||
|
Comment on lines
+69
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use |
||||||||||||||||||||
|
|
||||||||||||||||||||
| static constexpr size_t const N = 100; | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constexpr of a const
Suggested change
|
||||||||||||||||||||
| PDI_expose("N", &N, PDI_OUT); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| auto const test_array = make_a<std::array<std::array<double, N>, N>>(); | ||||||||||||||||||||
| PDI_expose("array", test_array.data(), PDI_OUT); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| EXPECT_TRUE(std::filesystem::exists("d2d_test.h5")); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, we should use assert her because if we don't have the file the comparison test
Suggested change
|
||||||||||||||||||||
| EXPECT_TRUE(std::filesystem::exists("d2f_test.h5")); | ||||||||||||||||||||
| EXPECT_TRUE(std::filesystem::exists("d2i_test.h5")); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // read double precision dataset and compare | ||||||||||||||||||||
| hid_t file_id = H5Fopen("d2d_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); | ||||||||||||||||||||
| hid_t dataset_id = H5Dopen2(file_id, "/double_ds", H5P_DEFAULT); | ||||||||||||||||||||
| hid_t type_id = H5Dget_type(dataset_id); | ||||||||||||||||||||
|
Comment on lines
+84
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May also check for read failure ?
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| EXPECT_TRUE(H5Tequal(type_id, H5T_IEEE_F64LE)); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error of value "-1" would pass this check ?
Suggested change
|
||||||||||||||||||||
| std::array<std::array<double, N>, N> read_double_array; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| herr_t status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, read_double_array.data()); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pointer is not only a double, as it includes the
Suggested change
|
||||||||||||||||||||
| ASSERT_GE(status, 0); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| EXPECT_EQ(test_array, read_double_array); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| H5Tclose(type_id); | ||||||||||||||||||||
| H5Dclose(dataset_id); | ||||||||||||||||||||
| H5Fclose(file_id); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // read simple precision dataset and compare | ||||||||||||||||||||
| file_id = H5Fopen("d2f_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); | ||||||||||||||||||||
| dataset_id = H5Dopen2(file_id, "/float_ds", H5P_DEFAULT); | ||||||||||||||||||||
| type_id = H5Dget_type(dataset_id); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| EXPECT_TRUE(H5Tequal(type_id, H5T_IEEE_F32LE)); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to EXPECT_TRUE issue of H5T_IEEE_F64LE |
||||||||||||||||||||
| std::array< std::array<float, N>, N > read_float_array; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| status = H5Dread(dataset_id, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, read_float_array.data()); | ||||||||||||||||||||
| ASSERT_GE(status, 0); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #if defined(__clang__) && (__clang_major__ == 15) | ||||||||||||||||||||
| // Skipping the numerical comparaison because Clang 15 has known issues with ranges. | ||||||||||||||||||||
| #else | ||||||||||||||||||||
| EXPECT_THAT(read_float_array, testing::ElementsAreArray(test_array | std::views::transform([](std::array<double, N> const & aref) { | ||||||||||||||||||||
| return testing::Pointwise(testing::FloatEq(), aref); | ||||||||||||||||||||
|
iole-bolognesi marked this conversation as resolved.
|
||||||||||||||||||||
| }))); | ||||||||||||||||||||
| #endif | ||||||||||||||||||||
|
|
||||||||||||||||||||
| H5Tclose(type_id); | ||||||||||||||||||||
| H5Dclose(dataset_id); | ||||||||||||||||||||
| H5Fclose(file_id); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // read integer dataset and compare | ||||||||||||||||||||
| file_id = H5Fopen("d2i_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); | ||||||||||||||||||||
| dataset_id = H5Dopen2(file_id, "/int_ds", H5P_DEFAULT); | ||||||||||||||||||||
| type_id = H5Dget_type(dataset_id); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| EXPECT_TRUE(H5Tequal(type_id, H5T_STD_I32LE)); | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to EXPECT_TRUE issue of H5T_IEEE_F64LE |
||||||||||||||||||||
| std::array< std::array<int, N>, N > read_int_array; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, read_int_array.data()); | ||||||||||||||||||||
| ASSERT_GE(status, 0); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| #if defined(__clang__) && (__clang_major__ == 15) | ||||||||||||||||||||
| // Skipping the numerical comparaison because Clang 15 has known issues with ranges. | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps do something like that in case we have issue with ranges, std::array<std::array<int, N>, N> expected_int_array;
for( int ii=0; ii<test_array.size(); ++ii) {
std::transform(test_array[ii].cbegin(), test_array[ii].cend(), expected_int_array[ii].begin(),
[](auto aa){ return static_cast<int>(aa); });
}
// comparison
EXPECT_EQ(expected_int_array, read_int_array); |
||||||||||||||||||||
| #else | ||||||||||||||||||||
| EXPECT_THAT(read_int_array, testing::ElementsAreArray(test_array | std::views::transform([](std::array<double, N> const & aref) { | ||||||||||||||||||||
| return testing::ElementsAreArray(aref | std::views::transform([](double const & ref) { | ||||||||||||||||||||
| return static_cast<int>(ref); | ||||||||||||||||||||
| })); | ||||||||||||||||||||
| }))); | ||||||||||||||||||||
| #endif | ||||||||||||||||||||
|
|
||||||||||||||||||||
| H5Tclose(type_id); | ||||||||||||||||||||
| H5Dclose(dataset_id); | ||||||||||||||||||||
| H5Fclose(file_id); | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do
GTest::gtest_mainandGTest::gmock_mainoverlink ? Two other files of the repo also use this