1 | #include "mlir/Analysis/Presburger/Fraction.h" |
2 | #include "./Utils.h" |
3 | #include <gmock/gmock.h> |
4 | #include <gtest/gtest.h> |
5 | |
6 | using namespace mlir; |
7 | using namespace presburger; |
8 | |
9 | TEST(FractionTest, getAsInteger) { |
10 | Fraction f(3, 1); |
11 | EXPECT_EQ(f.getAsInteger(), MPInt(3)); |
12 | } |
13 | |
14 | TEST(FractionTest, nearIntegers) { |
15 | Fraction f(52, 14); |
16 | |
17 | EXPECT_EQ(floor(f), 3); |
18 | EXPECT_EQ(ceil(f), 4); |
19 | } |
20 | |
21 | TEST(FractionTest, reduce) { |
22 | Fraction f(20, 35), g(-56, 63); |
23 | EXPECT_EQ(f, Fraction(4, 7)); |
24 | EXPECT_EQ(g, Fraction(-8, 9)); |
25 | } |
26 | |
27 | TEST(FractionTest, arithmetic) { |
28 | Fraction f(3, 4), g(-2, 3); |
29 | |
30 | EXPECT_EQ(f / g, Fraction(-9, 8)); |
31 | EXPECT_EQ(f * g, Fraction(-1, 2)); |
32 | EXPECT_EQ(f + g, Fraction(1, 12)); |
33 | EXPECT_EQ(f - g, Fraction(17, 12)); |
34 | |
35 | f /= g; |
36 | EXPECT_EQ(f, Fraction(-9, 8)); |
37 | f *= g; |
38 | EXPECT_EQ(f, Fraction(3, 4)); |
39 | f += g; |
40 | EXPECT_EQ(f, Fraction(Fraction(1, 12))); |
41 | f -= g; |
42 | EXPECT_EQ(f, Fraction(3, 4)); |
43 | } |
44 | |
45 | TEST(FractionTest, relational) { |
46 | Fraction f(2, 5), g(3, 7); |
47 | EXPECT_TRUE(f < g); |
48 | EXPECT_FALSE(g < f); |
49 | |
50 | EXPECT_EQ(f, Fraction(4, 10)); |
51 | } |
52 | |