Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / dio-hdf5 / src / HDF5Importer.test.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "dio_hdf5/HDF5Importer.h"
18
19 #include <loco.h>
20
21 #include <H5Cpp.h>
22
23 #include <cstdio>
24
25 #include <gtest/gtest.h>
26
27 using HDF5Importer = dio::hdf5::HDF5Importer;
28 using Shape = std::vector<loco::Dimension>;
29 using DataType = loco::DataType;
30
31 namespace
32 {
33
34 const std::string file_name("dio_hdf5_test.h5");
35
36 void createFile()
37 {
38   // File already exists. Remove it.
39   if (auto f = fopen(file_name.c_str(), "r"))
40   {
41     fclose(f);
42     if (remove(file_name.c_str()) != 0)
43       throw std::runtime_error("Error deleting file.");
44   }
45
46   const auto rank = 3;
47   hsize_t dim[3] = {1, 2, 3};
48   H5::DataSpace space(rank, dim);
49
50   float data[] = {0, 1, 2, 3, 4, 5};
51
52   // Create test file in the current directory
53   H5::H5File file(file_name, H5F_ACC_TRUNC);
54   {
55     file.createGroup("/value");
56     file.createGroup("/value/0");
57     H5::DataSet dataset(file.createDataSet("/value/0/0", H5::PredType::IEEE_F32BE, space));
58     dataset.write(data, H5::PredType::IEEE_F32LE);
59   }
60 }
61
62 } // namespace
63
64 TEST(dio_hdf5_test, read_with_type_shape)
65 {
66   createFile();
67
68   HDF5Importer h5(::file_name);
69
70   h5.importGroup("value");
71
72   std::vector<float> buffer(6);
73
74   DataType dtype;
75   Shape shape;
76   h5.readTensor(0, 0, &dtype, &shape, buffer.data(), buffer.size() * sizeof(float));
77
78   for (uint32_t i = 0; i < 6; i++)
79     EXPECT_EQ(i, buffer[i]);
80
81   EXPECT_EQ(DataType::FLOAT32, dtype);
82   EXPECT_EQ(3, shape.size());
83   EXPECT_EQ(1, shape[0]);
84   EXPECT_EQ(2, shape[1]);
85   EXPECT_EQ(3, shape[2]);
86 }
87
88 TEST(dio_hdf5_test, wrong_path_NEG)
89 {
90   const std::string wrong_path = "not_existing_file_for_dio_hdf5_test";
91
92   EXPECT_ANY_THROW(HDF5Importer h5(wrong_path));
93 }
94
95 TEST(dio_hdf5_test, wrong_group_name_NEG)
96 {
97   createFile();
98
99   HDF5Importer h5(::file_name);
100
101   EXPECT_ANY_THROW(h5.importGroup("wrong"));
102 }
103
104 TEST(dio_hdf5_test, data_out_of_index_NEG)
105 {
106   createFile();
107
108   HDF5Importer h5(::file_name);
109
110   h5.importGroup("value");
111
112   std::vector<float> buffer(6);
113
114   DataType dtype;
115   Shape shape;
116   // Read non-existing data (data_idx = 1)
117   EXPECT_ANY_THROW(
118     h5.readTensor(1, 0, &dtype, &shape, buffer.data(), buffer.size() * sizeof(float)));
119 }
120
121 TEST(dio_hdf5_test, input_out_of_index_NEG)
122 {
123   createFile();
124
125   HDF5Importer h5(::file_name);
126
127   h5.importGroup("value");
128
129   std::vector<float> buffer(6);
130
131   DataType dtype;
132   Shape shape;
133   // Read non-existing input (input_idx = 1)
134   EXPECT_ANY_THROW(
135     h5.readTensor(0, 1, &dtype, &shape, buffer.data(), buffer.size() * sizeof(float)));
136 }
137
138 TEST(dio_hdf5_test, wrong_buffer_size_NEG)
139 {
140   createFile();
141
142   HDF5Importer h5(::file_name);
143
144   h5.importGroup("value");
145
146   std::vector<float> buffer(6);
147
148   DataType dtype;
149   Shape shape;
150   EXPECT_ANY_THROW(h5.readTensor(0, 0, &dtype, &shape, buffer.data(), 1 /* wrong buffer size */));
151 }