Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: Source/platform/DecimalTest.cpp

Issue 1182703002: Fix unit test style in Source/platform/, part 1. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 30 matching lines...) Expand all
41 std::ostream& operator<<(std::ostream& os, const Decimal& decimal) 41 std::ostream& operator<<(std::ostream& os, const Decimal& decimal)
42 { 42 {
43 Decimal::EncodedData data = decimal.value(); 43 Decimal::EncodedData data = decimal.value();
44 return os 44 return os
45 << "encode(" << String::number(data.coefficient()).ascii().data() 45 << "encode(" << String::number(data.coefficient()).ascii().data()
46 << ", " << String::number(data.exponent()).ascii().data() 46 << ", " << String::number(data.exponent()).ascii().data()
47 << ", " << (data.sign() == Decimal::Negative ? "Negative" : "Positive") 47 << ", " << (data.sign() == Decimal::Negative ? "Negative" : "Positive")
48 << ")=" << decimal.toString().ascii().data(); 48 << ")=" << decimal.toString().ascii().data();
49 } 49 }
50 50
51 } // namespace blink
52
53 using namespace blink;
54
55 // Simulate WebCore/html/StepRange 51 // Simulate WebCore/html/StepRange
56 class DecimalStepRange { 52 class DecimalStepRange {
57 public: 53 public:
58 Decimal maximum; 54 Decimal maximum;
59 Decimal minimum; 55 Decimal minimum;
60 Decimal step; 56 Decimal step;
61 57
62 DecimalStepRange(const Decimal& minimum, const Decimal& maximum, const Decim al& step) 58 DecimalStepRange(const Decimal& minimum, const Decimal& maximum, const Decim al& step)
63 : maximum(maximum) 59 : maximum(maximum)
64 , minimum(minimum) 60 , minimum(minimum)
65 , step(step) 61 , step(step)
66 { 62 {
67 } 63 }
68 64
69 Decimal clampValue(Decimal value) const 65 Decimal clampValue(Decimal value) const
70 { 66 {
71 const Decimal result = minimum + ((value - minimum) / step).round() * st ep; 67 const Decimal result = minimum + ((value - minimum) / step).round() * st ep;
72 ASSERT(result.isFinite()); 68 ASSERT(result.isFinite());
73 return result > maximum ? result - step : result; 69 return result > maximum ? result - step : result;
74 } 70 }
75 }; 71 };
76 72
77 class DecimalTest : public ::testing::Test { 73 class DecimalTest : public ::testing::Test {
78 protected: 74 protected:
79 typedef Decimal::Sign Sign; 75 using Sign = Decimal::Sign;
80 protected: static const Sign Positive = Decimal::Positive; 76 static const Sign Positive = Decimal::Positive;
81 protected: static const Sign Negative = Decimal::Negative; 77 static const Sign Negative = Decimal::Negative;
82 78
83 Decimal encode(uint64_t coefficient, int exponent, Sign sign) 79 Decimal encode(uint64_t coefficient, int exponent, Sign sign)
84 { 80 {
85 return Decimal(sign, exponent, coefficient); 81 return Decimal(sign, exponent, coefficient);
86 } 82 }
87 83
88 protected: Decimal fromString(const String& string) 84 Decimal fromString(const String& string)
89 { 85 {
90 return Decimal::fromString(string); 86 return Decimal::fromString(string);
91 } 87 }
92 88
93 protected: Decimal stepDown(const String& minimum, const String& maximum, co nst String& step, const String& valueString, int numberOfStepTimes) 89 Decimal stepDown(const String& minimum, const String& maximum, const String& step, const String& valueString, int numberOfStepTimes)
94 { 90 {
95 DecimalStepRange stepRange(fromString(minimum), fromString(maximum), fro mString(step)); 91 DecimalStepRange stepRange(fromString(minimum), fromString(maximum), fro mString(step));
96 Decimal value = fromString(valueString); 92 Decimal value = fromString(valueString);
97 for (int i = 0; i < numberOfStepTimes; ++i) { 93 for (int i = 0; i < numberOfStepTimes; ++i) {
98 value -= stepRange.step; 94 value -= stepRange.step;
99 value = stepRange.clampValue(value); 95 value = stepRange.clampValue(value);
100 } 96 }
101 return value; 97 return value;
102 } 98 }
103 99
104 protected: Decimal stepUp(const String& minimum, const String& maximum, cons t String& step, const String& valueString, int numberOfStepTimes) 100 Decimal stepUp(const String& minimum, const String& maximum, const String& s tep, const String& valueString, int numberOfStepTimes)
105 { 101 {
106 DecimalStepRange stepRange(fromString(minimum), fromString(maximum), fro mString(step)); 102 DecimalStepRange stepRange(fromString(minimum), fromString(maximum), fro mString(step));
107 Decimal value = fromString(valueString); 103 Decimal value = fromString(valueString);
108 for (int i = 0; i < numberOfStepTimes; ++i) { 104 for (int i = 0; i < numberOfStepTimes; ++i) {
109 value += stepRange.step; 105 value += stepRange.step;
110 value = stepRange.clampValue(value); 106 value = stepRange.clampValue(value);
111 } 107 }
112 return value; 108 return value;
113 } 109 }
114 }; 110 };
(...skipping 1001 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 EXPECT_DECIMAL_STREQ("0.001", encode(UINT64_C(99999999999999999), -20, Posit ive)); 1112 EXPECT_DECIMAL_STREQ("0.001", encode(UINT64_C(99999999999999999), -20, Posit ive));
1117 EXPECT_DECIMAL_STREQ("1e-83", encode(UINT64_C(99999999999999999), -100, Posi tive)); 1113 EXPECT_DECIMAL_STREQ("1e-83", encode(UINT64_C(99999999999999999), -100, Posi tive));
1118 } 1114 }
1119 1115
1120 TEST_F(DecimalTest, ToStringSpecialValues) 1116 TEST_F(DecimalTest, ToStringSpecialValues)
1121 { 1117 {
1122 EXPECT_DECIMAL_STREQ("Infinity", Decimal::infinity(Positive)); 1118 EXPECT_DECIMAL_STREQ("Infinity", Decimal::infinity(Positive));
1123 EXPECT_DECIMAL_STREQ("-Infinity", Decimal::infinity(Negative)); 1119 EXPECT_DECIMAL_STREQ("-Infinity", Decimal::infinity(Negative));
1124 EXPECT_DECIMAL_STREQ("NaN", Decimal::nan()); 1120 EXPECT_DECIMAL_STREQ("NaN", Decimal::nan());
1125 } 1121 }
1122
1123 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698