File: simple_fraction_unittest.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (100 lines) | stat: -rw-r--r-- 3,758 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "util/simple_fraction.h"

#include <cmath>
#include <limits>

#include "gtest/gtest.h"

namespace openscreen {

namespace {

constexpr int kMin = std::numeric_limits<int>::min();
constexpr int kMax = std::numeric_limits<int>::max();

void ExpectFromStringEquals(const char* s, const SimpleFraction& expected) {
  const ErrorOr<SimpleFraction> f = SimpleFraction::FromString(std::string(s));
  EXPECT_TRUE(f.is_value()) << "from string: '" << s << "'";
  EXPECT_EQ(expected, f.value());
}

void ExpectFromStringError(const char* s) {
  const auto f = SimpleFraction::FromString(std::string(s));
  EXPECT_TRUE(f.is_error()) << "from string: '" << s << "'";
}
}  // namespace

TEST(SimpleFractionTest, FromStringParsesCorrectFractions) {
  ExpectFromStringEquals("1/2", SimpleFraction{1, 2});
  ExpectFromStringEquals("99/3", SimpleFraction{99, 3});
  ExpectFromStringEquals("-1/2", SimpleFraction{-1, 2});
  ExpectFromStringEquals("-13/-37", SimpleFraction{-13, -37});
  ExpectFromStringEquals("1/0", SimpleFraction{1, 0});
  ExpectFromStringEquals("1", SimpleFraction{1, 1});
  ExpectFromStringEquals("0", SimpleFraction{0, 1});
  ExpectFromStringEquals("-20", SimpleFraction{-20, 1});
  ExpectFromStringEquals("100", SimpleFraction{100, 1});
}

TEST(SimpleFractionTest, FromStringErrorsOnInvalid) {
  ExpectFromStringError("");
  ExpectFromStringError("/");
  ExpectFromStringError("1/");
  ExpectFromStringError("/1");
  ExpectFromStringError("888/");
  ExpectFromStringError("1/2/3");
  ExpectFromStringError("not a fraction at all");
}

TEST(SimpleFractionTest, Equality) {
  EXPECT_EQ((SimpleFraction{1, 2}), (SimpleFraction{1, 2}));
  EXPECT_EQ((SimpleFraction{1, 0}), (SimpleFraction{1, 0}));
  EXPECT_NE((SimpleFraction{1, 2}), (SimpleFraction{1, 3}));

  // We currently don't do any reduction.
  EXPECT_NE((SimpleFraction{2, 4}), (SimpleFraction{1, 2}));
  EXPECT_NE((SimpleFraction{9, 10}), (SimpleFraction{-9, -10}));
}

TEST(SimpleFractionTest, Definition) {
  EXPECT_TRUE((SimpleFraction{kMin, 1}).is_defined());
  EXPECT_TRUE((SimpleFraction{kMax, 1}).is_defined());

  EXPECT_FALSE((SimpleFraction{kMin, 0}).is_defined());
  EXPECT_FALSE((SimpleFraction{kMax, 0}).is_defined());
  EXPECT_FALSE((SimpleFraction{0, 0}).is_defined());
  EXPECT_FALSE((SimpleFraction{-0, -0}).is_defined());
}

TEST(SimpleFractionTest, Positivity) {
  EXPECT_TRUE((SimpleFraction{1234, 20}).is_positive());
  EXPECT_TRUE((SimpleFraction{kMax - 1, 20}).is_positive());
  EXPECT_TRUE((SimpleFraction{0, kMax}).is_positive());
  EXPECT_TRUE((SimpleFraction{kMax, 1}).is_positive());

  // Since C++ doesn't have a truly negative zero, this is positive.
  EXPECT_TRUE((SimpleFraction{-0, 1}).is_positive());

  EXPECT_FALSE((SimpleFraction{0, kMin}).is_positive());
  EXPECT_FALSE((SimpleFraction{-0, -1}).is_positive());
  EXPECT_FALSE((SimpleFraction{kMin + 1, 20}).is_positive());
  EXPECT_FALSE((SimpleFraction{kMin, 1}).is_positive());
  EXPECT_FALSE((SimpleFraction{kMin, 0}).is_positive());
  EXPECT_FALSE((SimpleFraction{kMax, 0}).is_positive());
  EXPECT_FALSE((SimpleFraction{0, 0}).is_positive());
  EXPECT_FALSE((SimpleFraction{-0, -0}).is_positive());
}

TEST(SimpleFractionTest, CastToDouble) {
  EXPECT_DOUBLE_EQ(0.0, static_cast<double>(SimpleFraction{0, 1}));
  EXPECT_DOUBLE_EQ(1.0, static_cast<double>(SimpleFraction{1, 1}));
  EXPECT_TRUE(std::isnan(static_cast<double>(SimpleFraction{1, 0})));
  EXPECT_DOUBLE_EQ(1.0, static_cast<double>(SimpleFraction{kMax, kMax}));
  EXPECT_DOUBLE_EQ(1.0, static_cast<double>(SimpleFraction{kMin, kMin}));
}

}  // namespace openscreen