1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "util/simple_fraction.h"
6
7 #include <cmath>
8 #include <limits>
9
10 #include "gtest/gtest.h"
11
12 namespace openscreen {
13
14 namespace {
15
16 constexpr int kMin = std::numeric_limits<int>::min();
17 constexpr int kMax = std::numeric_limits<int>::max();
18
ExpectFromStringEquals(const char * s,const SimpleFraction & expected)19 void ExpectFromStringEquals(const char* s, const SimpleFraction& expected) {
20 const ErrorOr<SimpleFraction> f = SimpleFraction::FromString(std::string(s));
21 EXPECT_TRUE(f.is_value()) << "from string: '" << s << "'";
22 EXPECT_EQ(expected, f.value());
23 }
24
ExpectFromStringError(const char * s)25 void ExpectFromStringError(const char* s) {
26 const auto f = SimpleFraction::FromString(std::string(s));
27 EXPECT_TRUE(f.is_error()) << "from string: '" << s << "'";
28 }
29 } // namespace
30
TEST(SimpleFractionTest,FromStringParsesCorrectFractions)31 TEST(SimpleFractionTest, FromStringParsesCorrectFractions) {
32 ExpectFromStringEquals("1/2", SimpleFraction{1, 2});
33 ExpectFromStringEquals("99/3", SimpleFraction{99, 3});
34 ExpectFromStringEquals("-1/2", SimpleFraction{-1, 2});
35 ExpectFromStringEquals("-13/-37", SimpleFraction{-13, -37});
36 ExpectFromStringEquals("1/0", SimpleFraction{1, 0});
37 ExpectFromStringEquals("1", SimpleFraction{1, 1});
38 ExpectFromStringEquals("0", SimpleFraction{0, 1});
39 ExpectFromStringEquals("-20", SimpleFraction{-20, 1});
40 ExpectFromStringEquals("100", SimpleFraction{100, 1});
41 }
42
TEST(SimpleFractionTest,FromStringErrorsOnInvalid)43 TEST(SimpleFractionTest, FromStringErrorsOnInvalid) {
44 ExpectFromStringError("");
45 ExpectFromStringError("/");
46 ExpectFromStringError("1/");
47 ExpectFromStringError("/1");
48 ExpectFromStringError("888/");
49 ExpectFromStringError("1/2/3");
50 ExpectFromStringError("not a fraction at all");
51 }
52
TEST(SimpleFractionTest,Equality)53 TEST(SimpleFractionTest, Equality) {
54 EXPECT_EQ((SimpleFraction{1, 2}), (SimpleFraction{1, 2}));
55 EXPECT_EQ((SimpleFraction{1, 0}), (SimpleFraction{1, 0}));
56 EXPECT_NE((SimpleFraction{1, 2}), (SimpleFraction{1, 3}));
57
58 // We currently don't do any reduction.
59 EXPECT_NE((SimpleFraction{2, 4}), (SimpleFraction{1, 2}));
60 EXPECT_NE((SimpleFraction{9, 10}), (SimpleFraction{-9, -10}));
61 }
62
TEST(SimpleFractionTest,Definition)63 TEST(SimpleFractionTest, Definition) {
64 EXPECT_TRUE((SimpleFraction{kMin, 1}).is_defined());
65 EXPECT_TRUE((SimpleFraction{kMax, 1}).is_defined());
66
67 EXPECT_FALSE((SimpleFraction{kMin, 0}).is_defined());
68 EXPECT_FALSE((SimpleFraction{kMax, 0}).is_defined());
69 EXPECT_FALSE((SimpleFraction{0, 0}).is_defined());
70 EXPECT_FALSE((SimpleFraction{-0, -0}).is_defined());
71 }
72
TEST(SimpleFractionTest,Positivity)73 TEST(SimpleFractionTest, Positivity) {
74 EXPECT_TRUE((SimpleFraction{1234, 20}).is_positive());
75 EXPECT_TRUE((SimpleFraction{kMax - 1, 20}).is_positive());
76 EXPECT_TRUE((SimpleFraction{0, kMax}).is_positive());
77 EXPECT_TRUE((SimpleFraction{kMax, 1}).is_positive());
78
79 // Since C++ doesn't have a truly negative zero, this is positive.
80 EXPECT_TRUE((SimpleFraction{-0, 1}).is_positive());
81
82 EXPECT_FALSE((SimpleFraction{0, kMin}).is_positive());
83 EXPECT_FALSE((SimpleFraction{-0, -1}).is_positive());
84 EXPECT_FALSE((SimpleFraction{kMin + 1, 20}).is_positive());
85 EXPECT_FALSE((SimpleFraction{kMin, 1}).is_positive());
86 EXPECT_FALSE((SimpleFraction{kMin, 0}).is_positive());
87 EXPECT_FALSE((SimpleFraction{kMax, 0}).is_positive());
88 EXPECT_FALSE((SimpleFraction{0, 0}).is_positive());
89 EXPECT_FALSE((SimpleFraction{-0, -0}).is_positive());
90 }
91
TEST(SimpleFractionTest,CastToDouble)92 TEST(SimpleFractionTest, CastToDouble) {
93 EXPECT_DOUBLE_EQ(0.0, static_cast<double>(SimpleFraction{0, 1}));
94 EXPECT_DOUBLE_EQ(1.0, static_cast<double>(SimpleFraction{1, 1}));
95 EXPECT_TRUE(std::isnan(static_cast<double>(SimpleFraction{1, 0})));
96 EXPECT_DOUBLE_EQ(1.0, static_cast<double>(SimpleFraction{kMax, kMax}));
97 EXPECT_DOUBLE_EQ(1.0, static_cast<double>(SimpleFraction{kMin, kMin}));
98 }
99
100 } // namespace openscreen
101