// Copyright 2021 https://github.com/tlepoint // // 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 "barrett.h" #include "absl/numeric/int128.h" #include "gtest/gtest.h" #include "openssl/rand.h" namespace { template class BarrettTest : public testing::Test { protected: void SetUp() { values_.resize(number_tests_); for (int i = 0; i < number_tests_; i++) { RAND_bytes(reinterpret_cast(&values_[i]), sizeof(values_[i])); } } std::vector values_; const int number_tests_ = 100; }; typedef testing::Types IntTypes; TYPED_TEST_SUITE(BarrettTest, IntTypes); TYPED_TEST(BarrettTest, Correctness) { for (TypeParam modulus : {static_cast(1) << 2, static_cast(1) << 8, (static_cast(-1) >> 6)}) { Barrett barrett(modulus); std::vector out = barrett.Reduce(this->values_); for (int i = 0; i < this->number_tests_; i++) { ASSERT_EQ(out[i], (this->values_[i] % modulus)); } } } } // namespace int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }