From 553fa730de69ba93c2a2249bdc9b5a59166e84a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Tue, 26 Sep 2017 18:19:40 +0200 Subject: [PATCH] [poincare] add tests on arithmetic functions Change-Id: Iac54616ebf8281179b5e832003a643da9122b007 --- poincare/Makefile | 3 +- poincare/include/poincare/integer.h | 2 +- poincare/test/arithmetic.cpp | 65 +++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 poincare/test/arithmetic.cpp diff --git a/poincare/Makefile b/poincare/Makefile index 128627af7..fcb87b749 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -98,12 +98,13 @@ objs += $(addprefix poincare/src/layout/,\ sum_layout.o\ ) -tests += $(addprefix poincare/test/,\ +testsi += $(addprefix poincare/test/,\ simplify_easy.cpp\ ) tests += $(addprefix poincare/test/,\ addition.cpp\ + arithmetic.cpp\ complex.cpp\ division.cpp\ function.cpp\ diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 51c3c2da9..8e6b1b856 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -16,7 +16,7 @@ public: typedef uint64_t double_native_uint_t; // FIXME: This constructor should be constexpr - Integer(native_int_t i) : + Integer(native_int_t i = 0) : StaticHierarchy<0>(), m_digit(i>0 ? i : -i), m_numberOfDigits(1), diff --git a/poincare/test/arithmetic.cpp b/poincare/test/arithmetic.cpp new file mode 100644 index 000000000..124074133 --- /dev/null +++ b/poincare/test/arithmetic.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +#if POINCARE_TESTS_PRINT_EXPRESSIONS +#include "../src/expression_debug.h" +#include +using namespace std; +#endif + +using namespace Poincare; + +void assert_gcd_equals_to(Integer a, Integer b, Integer c) { + GlobalContext context; +#if POINCARE_TESTS_PRINT_EXPRESSIONS + cout << "---- GCD ----" << endl; + cout << "gcd(" << a.approximate(context); + cout << ", " << b.approximate(context) << ") = "; +#endif + Integer gcd = Arithmetic::GCD(std::move(a), std::move(b)); +#if POINCARE_TESTS_PRINT_EXPRESSIONS + cout << gcd.approximate(context) << endl; +#endif + assert(gcd.compareTo(&c) == 0); +} + +void assert_prime_factorization_equals_to(Integer a, int * factors, int * coefficients, int length) { + GlobalContext context; + Integer outputFactors[1000]; + Integer outputCoefficients[1000]; +#if POINCARE_TESTS_PRINT_EXPRESSIONS + cout << "---- Primes factorization ----" << endl; + cout << "Decomp(" << a.approximate(context) << ") = "; +#endif + Arithmetic::PrimeFactorization(std::move(a), outputFactors, outputCoefficients, 10); +#if POINCARE_TESTS_PRINT_EXPRESSIONS + print_prime_factorization(outputFactors, outputCoefficients, 10); +#endif + for (int index = 0; index < length; index++) { + if (outputCoefficients[index].isEqualTo(Integer(0))) { + break; + } + assert(outputFactors[index].identifier() == factors[index]); // Cheat: instead of comparing to integers, we compare only identifier as we know that prime factors and their coefficients will always be lower than 2^32. + assert(outputCoefficients[index].identifier() == coefficients[index]); + } +} + +QUIZ_CASE(poincare_arithmetic) { + assert_gcd_equals_to(Integer(11), Integer(121), Integer(11)); + assert_gcd_equals_to(Integer(-256), Integer(321), Integer(1)); + assert_gcd_equals_to(Integer(-8), Integer(-40), Integer(8)); + assert_gcd_equals_to(Integer("1234567899876543456", true), Integer("234567890098765445678"), Integer(2)); + assert_gcd_equals_to(Integer("45678998789"), Integer("1461727961248"), Integer("45678998789")); + int factors0[5] = {2,3,5,79,1319}; + int coefficients0[5] = {2,1,1,1,1}; + assert_prime_factorization_equals_to(Integer(6252060), factors0, coefficients0, 5); + int factors1[3] = {3,2969, 6907}; + int coefficients1[3] = {1,1,1}; + assert_prime_factorization_equals_to(Integer(61520649), factors1, coefficients1, 3); + int factors2[3] = {2,5, 7}; + int coefficients2[3] = {2,4,2}; + assert_prime_factorization_equals_to(Integer(122500), factors2, coefficients2, 3); +}