Skip to content

Commit

Permalink
codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
Semyon1104 committed Aug 8, 2024
1 parent 9fd09af commit d90d5f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions include/Weights_Reader/reader_weights.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ void extract_values_from_json(const json& j, std::vector<float>& values);
void parse_json_shape(const json& j, std::vector<size_t>& shape,
size_t dim = 0);
Tensor create_tensor_from_json(const json& j, Type type);
void extract_bias_from_json(const json& j, std::vector<float>& bias);
7 changes: 0 additions & 7 deletions src/Weights_Reader/reader_weights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ json read_json(const std::string& filename) {

ifs.seekg(0, std::ios::end);
size_t size = ifs.tellg();
if (size == 0) {
ifs.close();
}
ifs.seekg(0, std::ios::beg);

std::vector<char> buffer(size);
Expand Down Expand Up @@ -56,8 +53,6 @@ void extract_values_without_bias(const json& j, std::vector<float>& values) {
std::cout << "Bias size: " << bias_size << std::endl;
if (temp_values.size() >= bias_size) {
values.assign(temp_values.begin(), temp_values.end() - bias_size);
} else {
throw std::runtime_error("Extracted values are smaller than bias size.");
}
std::cout << "Values size after extraction: " << values.size() << std::endl;
}
Expand Down Expand Up @@ -94,8 +89,6 @@ void extract_bias_from_json(const json& j, std::vector<float>& bias) {
}
}
}
} else {
throw std::runtime_error("Input JSON structure should be an array.");
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/model_read/model_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,40 @@ TEST(ExtractValuesWithoutBiasTest, HandlesComplexNestedCase) {
std::vector<float> expected = {1.0, 2.0, 3.0, 4.0};
EXPECT_EQ(values, expected);
}

TEST(ExtractBiasFromJsonTests, extract_bias) {
json j = json::array({{1.0, 2.0}, {3.0, 4.0}});
std::vector<float> values;
extract_bias_from_json(j, values);
std::vector<float> expected = {3.0, 4.0};
EXPECT_EQ(values, expected);
}

TEST(ExtractBiasFromJsonTests, extract_bias_error_array) {
json j = json::array({{1.0, 2.0}, {3.0, 4.0}});
std::vector<float> values;
extract_bias_from_json(j, values);
std::vector<float> expected = {3.0, 4.0};
EXPECT_EQ(values, expected);
}

TEST(CreateTensorFromJsonTest, SimpleTensor) {
json j = json::array({{1.0, 2.0}, {3.0, 4.0}});
EXPECT_NO_THROW(Tensor tensor = create_tensor_from_json(j, Type::kFloat););
}

TEST(CreateTensorFromJsonTest, SimpleTensorCheckBias) {
json j = json::array({{1.0, 2.0}, {3.0, 4.0}});
Tensor tensor = create_tensor_from_json(j, Type::kFloat);

EXPECT_EQ(tensor.get_bias().size(), 2);
EXPECT_EQ(tensor.get_bias()[0], 3.0);
EXPECT_EQ(tensor.get_bias()[1], 4.0);
}

TEST(CreateTensorFromJsonTest, SimpleTensorCheckWeights) {
json j = json::array({{1.0, 2.0}, {3.0, 4.0}});
Tensor tensor = create_tensor_from_json(j, Type::kFloat);

EXPECT_EQ(tensor.get<float>({1}), 2.0);
}

0 comments on commit d90d5f8

Please sign in to comment.