From 3ef34fb300e7f62bdb397c605ab6c3bd30682cf8 Mon Sep 17 00:00:00 2001 From: Tai Ly Date: Tue, 4 Apr 2023 20:34:05 +0000 Subject: Add readFromNpyFile and writeToNpyFile for double data Signed-off-by: Tai Ly Change-Id: Icc023cbe6aa8843cc37d25e740bc6ce05bb7abb2 --- test/scripts/test_npy_fileio.py | 2 +- test/src/serialization_npy_test.cpp | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/scripts/test_npy_fileio.py b/test/scripts/test_npy_fileio.py index e0a6f5d..272c124 100755 --- a/test/scripts/test_npy_fileio.py +++ b/test/scripts/test_npy_fileio.py @@ -122,7 +122,7 @@ def main(): xunit_suite = xunit_result.create_suite("basic_serialization") max_size = 128 - datatypes = ["int32", "int64", "float", "bool"] + datatypes = ["int32", "int64", "float", "bool", "double"] random.seed(args.seed) failed = 0 diff --git a/test/src/serialization_npy_test.cpp b/test/src/serialization_npy_test.cpp index 27ec464..64536fb 100644 --- a/test/src/serialization_npy_test.cpp +++ b/test/src/serialization_npy_test.cpp @@ -103,6 +103,45 @@ int test_float_type(std::vector shape, std::default_random_engine& gen, return 0; } +template +int test_double_type(std::vector shape, std::default_random_engine& gen, std::string& filename) +{ + size_t total_size = 1; + std::uniform_real_distribution gen_data(std::numeric_limits::min(), std::numeric_limits::max()); + + for (auto i : shape) + { + total_size *= i; + } + + auto buffer = std::make_unique(total_size); + for (int i = 0; i < total_size; i++) + { + buffer[i] = gen_data(gen); + } + + NumpyUtilities::NPError err = NumpyUtilities::writeToNpyFile(filename.c_str(), shape, buffer.get()); + if (err != NumpyUtilities::NO_ERROR) + { + std::cout << "Error writing file, code " << err << std::endl; + return 1; + } + + auto read_buffer = std::make_unique(total_size); + err = NumpyUtilities::readFromNpyFile(filename.c_str(), total_size, read_buffer.get()); + if (err != NumpyUtilities::NO_ERROR) + { + std::cout << "Error reading file, code " << err << std::endl; + return 1; + } + if (memcmp(buffer.get(), read_buffer.get(), total_size * sizeof(T))) + { + std::cout << "Miscompare" << std::endl; + return 1; + } + return 0; +} + int test_bool_type(std::vector shape, std::default_random_engine& gen, std::string& filename) { size_t total_size = 1; @@ -212,6 +251,10 @@ int main(int argc, char** argv) { return test_float_type(shape, gen, filename); } + else if (str_type == "double") + { + return test_double_type(shape, gen, filename); + } else if (str_type == "bool") { return test_bool_type(shape, gen, filename); -- cgit v1.2.1