Branch data Line data Source code
1 : : /******************************************************************************
2 : : * Top contributors (to current version):
3 : : * Aina Niemetz, Gereon Kremer, Andrew Reynolds
4 : : *
5 : : * This file is part of the cvc5 project.
6 : : *
7 : : * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
8 : : * in the top-level source directory and their institutional affiliations.
9 : : * All rights reserved. See the file COPYING in the top-level source
10 : : * directory for licensing information.
11 : : * ****************************************************************************
12 : : *
13 : : * Black box testing of cvc5::RealAlgebraicNumber.
14 : : */
15 : :
16 : : #include "test.h"
17 : : #include "util/real_algebraic_number.h"
18 : :
19 : : namespace cvc5::internal {
20 : : namespace test {
21 : :
22 : : #ifndef CVC5_POLY_IMP
23 : : #error "This unit test should only be enabled for CVC5_POLY_IMP"
24 : : #endif
25 : :
26 : : class TestUtilBlackRealAlgebraicNumber : public TestInternal
27 : : {
28 : : };
29 : :
30 : 2 : TEST_F(TestUtilBlackRealAlgebraicNumber, creation)
31 : : {
32 [ - + ]: 1 : ASSERT_TRUE(RealAlgebraicNumber().isZero());
33 [ - + ]: 1 : ASSERT_TRUE(RealAlgebraicNumber(Integer(1)).isOne());
34 [ - + ]: 1 : ASSERT_FALSE(RealAlgebraicNumber(Rational(2)).isOne());
35 : 2 : RealAlgebraicNumber sqrt2({-2, 0, 1}, 1, 2);
36 [ - + ]: 1 : ASSERT_TRUE(RealAlgebraicNumber(Integer(1)) < sqrt2);
37 [ - + ]: 1 : ASSERT_TRUE(sqrt2 < RealAlgebraicNumber(Integer(2)));
38 : : }
39 : :
40 : 2 : TEST_F(TestUtilBlackRealAlgebraicNumber, comparison)
41 : : {
42 : 2 : RealAlgebraicNumber msqrt3({-3, 0, 1}, -2, -1);
43 : 2 : RealAlgebraicNumber msqrt2({-2, 0, 1}, -2, -1);
44 : 1 : RealAlgebraicNumber zero;
45 : 2 : RealAlgebraicNumber sqrt2({-2, 0, 1}, 1, 2);
46 : 2 : RealAlgebraicNumber sqrt3({-3, 0, 1}, 1, 2);
47 : :
48 [ - + ]: 1 : ASSERT_TRUE(msqrt3 < msqrt2);
49 [ - + ]: 1 : ASSERT_TRUE(msqrt3 < zero);
50 [ - + ]: 1 : ASSERT_TRUE(msqrt3 < sqrt2);
51 [ - + ]: 1 : ASSERT_TRUE(msqrt3 < sqrt3);
52 [ - + ]: 1 : ASSERT_TRUE(msqrt2 < zero);
53 [ - + ]: 1 : ASSERT_TRUE(msqrt2 < sqrt2);
54 [ - + ]: 1 : ASSERT_TRUE(msqrt2 < sqrt3);
55 [ - + ]: 1 : ASSERT_TRUE(zero < sqrt2);
56 [ - + ]: 1 : ASSERT_TRUE(zero < sqrt3);
57 [ - + ]: 1 : ASSERT_TRUE(sqrt2 < sqrt3);
58 : : }
59 : :
60 : 2 : TEST_F(TestUtilBlackRealAlgebraicNumber, sgn)
61 : : {
62 : 2 : RealAlgebraicNumber msqrt2({-2, 0, 1}, -2, -1);
63 : 1 : RealAlgebraicNumber zero;
64 : 2 : RealAlgebraicNumber sqrt2({-2, 0, 1}, 1, 2);
65 : :
66 [ - + ]: 1 : ASSERT_EQ(msqrt2.sgn(), -1);
67 [ - + ]: 1 : ASSERT_EQ(zero.sgn(), 0);
68 [ - + ]: 1 : ASSERT_EQ(sqrt2.sgn(), 1);
69 : : }
70 : :
71 : 2 : TEST_F(TestUtilBlackRealAlgebraicNumber, arithmetic)
72 : : {
73 : 2 : RealAlgebraicNumber msqrt2({-2, 0, 1}, -2, -1);
74 : 1 : RealAlgebraicNumber zero;
75 : 2 : RealAlgebraicNumber sqrt2({-2, 0, 1}, 1, 2);
76 : :
77 [ - + ]: 2 : ASSERT_EQ(msqrt2 + sqrt2, zero);
78 [ - + ]: 2 : ASSERT_EQ(-msqrt2, sqrt2);
79 [ - + ]: 2 : ASSERT_EQ(-msqrt2 + sqrt2, sqrt2 + sqrt2);
80 [ - + ]: 2 : ASSERT_EQ(msqrt2 * sqrt2, RealAlgebraicNumber(Integer(-2)));
81 : : }
82 : :
83 : 2 : TEST_F(TestUtilBlackRealAlgebraicNumber, division)
84 : : {
85 : 2 : RealAlgebraicNumber msqrt2({-2, 0, 1}, -2, -1);
86 : 2 : RealAlgebraicNumber sqrt2({-2, 0, 1}, 1, 2);
87 : 2 : RealAlgebraicNumber mone({1, 1}, -2, 0);
88 : :
89 [ - + ]: 2 : ASSERT_EQ(msqrt2 / sqrt2, mone);
90 [ - + ]: 1 : ASSERT_TRUE((sqrt2 / sqrt2).isOne());
91 : : }
92 : :
93 : : } // namespace test
94 : : } // namespace cvc5::internal
|