// Copyright 2025-present the zvec project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include #include #include #include #include #include #include using namespace zvec::ailego; TEST(Normalizer, FP32_General) { std::mt19937 gen((std::random_device())()); std::uniform_real_distribution dist(0.0, 1.0); for (size_t i = 0; i < 100; ++i) { std::vector vec1; std::vector vec2; for (size_t j = 0; j < 111; ++j) { float val = dist(gen); vec1.push_back(val); vec2.push_back(val); } Normalizer::Compute(vec1.data(), vec1.size(), 1.1f); for (size_t j = 0; j < vec1.size(); ++j) { EXPECT_FLOAT_EQ(vec1[j] * 1.1f, vec2[j]); } float l1 = 0.0f, l2 = 0.0f; Normalizer::L1(vec1.data(), vec1.size(), &l1); Normalizer::L2(vec2.data(), vec2.size(), &l2); } } TEST(Normalizer, FP16_General) { std::mt19937 gen((std::random_device())()); std::uniform_real_distribution dist(0.0, 1.0); for (size_t i = 0; i < 100; ++i) { std::vector vec1; std::vector vec2; for (size_t j = 0; j < 111; ++j) { float val = dist(gen); vec1.push_back(val); vec2.push_back(val); } Normalizer::Compute(vec1.data(), vec1.size(), 1.0f); for (size_t j = 0; j < vec1.size(); ++j) { EXPECT_FLOAT_EQ(vec1[j], vec2[j]); } float l1 = 0.0f, l2 = 0.0f; Normalizer::L1(vec1.data(), vec1.size(), &l1); Normalizer::L2(vec2.data(), vec2.size(), &l2); } } TEST(Normalizer, FP32_Zero) { std::mt19937 gen((std::random_device())()); std::uniform_int_distribution dist(1, 128); const uint32_t dimension = dist(gen); std::vector vec1(dimension, 0.0f); std::vector vec2(dimension, 0.0f); float norm; Normalizer::L1(vec1.data(), vec1.size(), &norm); Normalizer::L2(vec2.data(), vec2.size(), &norm); for (auto v : vec1) { EXPECT_FALSE(std::isnan(v)); } for (auto v : vec2) { EXPECT_FALSE(std::isnan(v)); } } TEST(Normalizer, FP16_Zero) { std::mt19937 gen((std::random_device())()); std::uniform_int_distribution dist(1, 128); const uint32_t dimension = dist(gen); std::vector vec1(dimension, 0.0f); std::vector vec2(dimension, 0.0f); float norm; Normalizer::L2(vec1.data(), vec1.size(), &norm); Normalizer::L2(vec2.data(), vec2.size(), &norm); for (auto v : vec1) { EXPECT_FALSE(std::isnan(v)); } for (auto v : vec2) { EXPECT_FALSE(std::isnan(v)); } }