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

Side by Side Diff: lib/math/math_test.dart

Issue 10797015: - Add basic math tests from math_test.dart, but using dart:math. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/tests/
Patch Set: Created 8 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #import("dart:math");
6
5 class MathTest { 7 class MathTest {
6 static void testConstants() { 8 static void testConstants() {
7 // Source for mathematical constants is Wolfram Alpha. 9 // Source for mathematical constants is Wolfram Alpha.
8 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, 10 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669,
9 Math.E); 11 E);
10 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, 12 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333,
11 Math.LN10); 13 LN10);
12 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206, 14 Expect.equals(0.6931471805599453094172321214581765680755001343602552541206,
13 Math.LN2); 15 LN2);
14 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354, 16 Expect.equals(1.4426950408889634073599246810018921374266459541529859341354,
15 Math.LOG2E); 17 LOG2E);
16 Expect.equals(0.4342944819032518276511289189166050822943970058036665661144, 18 Expect.equals(0.4342944819032518276511289189166050822943970058036665661144,
17 Math.LOG10E); 19 LOG10E);
18 Expect.equals(3.1415926535897932384626433832795028841971693993751058209749, 20 Expect.equals(3.1415926535897932384626433832795028841971693993751058209749,
19 Math.PI); 21 PI);
20 Expect.equals(0.7071067811865475244008443621048490392848359376884740365883, 22 Expect.equals(0.7071067811865475244008443621048490392848359376884740365883,
21 Math.SQRT1_2); 23 SQRT1_2);
22 Expect.equals(1.4142135623730950488016887242096980785696718753769480731766, 24 Expect.equals(1.4142135623730950488016887242096980785696718753769480731766,
23 Math.SQRT2); 25 SQRT2);
24 } 26 }
25 27
26 static checkClose(double a, double b, EPSILON) { 28 static checkClose(double a, double b, EPSILON) {
27 Expect.equals(true, a - EPSILON <= b); 29 Expect.equals(true, a - EPSILON <= b);
28 Expect.equals(true, b <= a + EPSILON); 30 Expect.equals(true, b <= a + EPSILON);
29 } 31 }
30 32
31 static void testSin() { 33 static void testSin() {
32 // Given the imprecision of PI we can't expect better results than this. 34 // Given the imprecision of PI we can't expect better results than this.
33 final double EPSILON = 1e-15; 35 final double EPSILON = 1e-15;
34 checkClose(0.0, Math.sin(0.0), EPSILON); 36 checkClose(0.0, sin(0.0), EPSILON);
35 checkClose(0.0, Math.sin(Math.PI), EPSILON); 37 checkClose(0.0, sin(PI), EPSILON);
36 checkClose(0.0, Math.sin(2.0 * Math.PI), EPSILON); 38 checkClose(0.0, sin(2.0 * PI), EPSILON);
37 checkClose(1.0, Math.sin(Math.PI / 2.0), EPSILON); 39 checkClose(1.0, sin(PI / 2.0), EPSILON);
38 checkClose(-1.0, Math.sin(Math.PI * (3.0 / 2.0)), EPSILON); 40 checkClose(-1.0, sin(PI * (3.0 / 2.0)), EPSILON);
39 } 41 }
40 42
41 static void testCos() { 43 static void testCos() {
42 // Given the imprecision of PI we can't expect better results than this. 44 // Given the imprecision of PI we can't expect better results than this.
43 final double EPSILON = 1e-15; 45 final double EPSILON = 1e-15;
44 checkClose(1.0, Math.cos(0.0), EPSILON); 46 checkClose(1.0, cos(0.0), EPSILON);
45 checkClose(-1.0, Math.cos(Math.PI), EPSILON); 47 checkClose(-1.0, cos(PI), EPSILON);
46 checkClose(1.0, Math.cos(2.0 * Math.PI), EPSILON); 48 checkClose(1.0, cos(2.0 * PI), EPSILON);
47 checkClose(0.0, Math.cos(Math.PI / 2.0), EPSILON); 49 checkClose(0.0, cos(PI / 2.0), EPSILON);
48 checkClose(0.0, Math.cos(Math.PI * (3.0 / 2.0)), EPSILON); 50 checkClose(0.0, cos(PI * (3.0 / 2.0)), EPSILON);
49 } 51 }
50 52
51 static void testTan() { 53 static void testTan() {
52 // Given the imprecision of PI we can't expect better results than this. 54 // Given the imprecision of PI we can't expect better results than this.
53 final double EPSILON = 1e-15; 55 final double EPSILON = 1e-15;
54 checkClose(0.0, Math.tan(0.0), EPSILON); 56 checkClose(0.0, tan(0.0), EPSILON);
55 checkClose(0.0, Math.tan(Math.PI), EPSILON); 57 checkClose(0.0, tan(PI), EPSILON);
56 checkClose(0.0, Math.tan(2.0 * Math.PI), EPSILON); 58 checkClose(0.0, tan(2.0 * PI), EPSILON);
57 checkClose(1.0, Math.tan(Math.PI / 4.0), EPSILON); 59 checkClose(1.0, tan(PI / 4.0), EPSILON);
58 } 60 }
59 61
60 static void testAsin() { 62 static void testAsin() {
61 // Given the imprecision of PI we can't expect better results than this. 63 // Given the imprecision of PI we can't expect better results than this.
62 final double EPSILON = 1e-15; 64 final double EPSILON = 1e-15;
63 checkClose(0.0, Math.asin(0.0), EPSILON); 65 checkClose(0.0, asin(0.0), EPSILON);
64 checkClose(Math.PI / 2.0, Math.asin(1.0), EPSILON); 66 checkClose(PI / 2.0, asin(1.0), EPSILON);
65 checkClose(-Math.PI / 2.0, Math.asin(-1.0), EPSILON); 67 checkClose(-PI / 2.0, asin(-1.0), EPSILON);
66 } 68 }
67 69
68 70
69 static void testAcos() { 71 static void testAcos() {
70 // Given the imprecision of PI we can't expect better results than this. 72 // Given the imprecision of PI we can't expect better results than this.
71 final double EPSILON = 1e-15; 73 final double EPSILON = 1e-15;
72 checkClose(0.0, Math.acos(1.0), EPSILON); 74 checkClose(0.0, acos(1.0), EPSILON);
73 checkClose(Math.PI, Math.acos(-1.0), EPSILON); 75 checkClose(PI, acos(-1.0), EPSILON);
74 checkClose(Math.PI / 2.0, Math.acos(0.0), EPSILON); 76 checkClose(PI / 2.0, acos(0.0), EPSILON);
75 } 77 }
76 78
77 static void testAtan() { 79 static void testAtan() {
78 // Given the imprecision of PI we can't expect better results than this. 80 // Given the imprecision of PI we can't expect better results than this.
79 final double EPSILON = 1e-15; 81 final double EPSILON = 1e-15;
80 checkClose(0.0, Math.atan(0.0), EPSILON); 82 checkClose(0.0, atan(0.0), EPSILON);
81 checkClose(Math.PI / 4.0, Math.atan(1.0), EPSILON); 83 checkClose(PI / 4.0, atan(1.0), EPSILON);
82 checkClose(-Math.PI / 4.0, Math.atan(-1.0), EPSILON); 84 checkClose(-PI / 4.0, atan(-1.0), EPSILON);
83 } 85 }
84 86
85 static void testAtan2() { 87 static void testAtan2() {
86 // Given the imprecision of PI we can't expect better results than this. 88 // Given the imprecision of PI we can't expect better results than this.
87 final double EPSILON = 1e-15; 89 final double EPSILON = 1e-15;
88 checkClose(0.0, Math.atan2(0.0, 5.0), EPSILON); 90 checkClose(0.0, atan2(0.0, 5.0), EPSILON);
89 checkClose(Math.PI / 4.0, Math.atan2(2.0, 2.0), EPSILON); 91 checkClose(PI / 4.0, atan2(2.0, 2.0), EPSILON);
90 checkClose(3 * Math.PI / 4.0, Math.atan2(0.5, -0.5), EPSILON); 92 checkClose(3 * PI / 4.0, atan2(0.5, -0.5), EPSILON);
91 checkClose(-3 * Math.PI / 4.0, Math.atan2(-2.5, -2.5), EPSILON); 93 checkClose(-3 * PI / 4.0, atan2(-2.5, -2.5), EPSILON);
92 } 94 }
93 95
94 static checkVeryClose(double a, double b) { 96 static checkVeryClose(double a, double b) {
95 // We find a ulp (unit in the last place) by shifting the original number 97 // We find a ulp (unit in the last place) by shifting the original number
96 // to the right. This only works if we are not too close to infinity or if 98 // to the right. This only works if we are not too close to infinity or if
97 // we work with denormals. 99 // we work with denormals.
98 // We special case or 0.0, but not for infinity. 100 // We special case or 0.0, but not for infinity.
99 if (a == 0.0) { 101 if (a == 0.0) {
100 final minimalDouble = 4.9406564584124654e-324; 102 final minimalDouble = 4.9406564584124654e-324;
101 Expect.equals(true, b.abs() <= minimalDouble); 103 Expect.equals(true, b.abs() <= minimalDouble);
(...skipping 10 matching lines...) Expand all
112 114
113 final double limitLow = a - shiftedA; 115 final double limitLow = a - shiftedA;
114 final double limitHigh = a + shiftedA; 116 final double limitHigh = a + shiftedA;
115 Expect.equals(false, a == limitLow); 117 Expect.equals(false, a == limitLow);
116 Expect.equals(false, a == limitHigh); 118 Expect.equals(false, a == limitHigh);
117 Expect.equals(true, limitLow <= b); 119 Expect.equals(true, limitLow <= b);
118 Expect.equals(true, b <= limitHigh); 120 Expect.equals(true, b <= limitHigh);
119 } 121 }
120 122
121 static void testSqrt() { 123 static void testSqrt() {
122 checkVeryClose(2.0, Math.sqrt(4.0)); 124 checkVeryClose(2.0, sqrt(4.0));
123 checkVeryClose(Math.SQRT2, Math.sqrt(2.0)); 125 checkVeryClose(SQRT2, sqrt(2.0));
124 checkVeryClose(Math.SQRT1_2, Math.sqrt(0.5)); 126 checkVeryClose(SQRT1_2, sqrt(0.5));
125 checkVeryClose(1e50, Math.sqrt(1e100)); 127 checkVeryClose(1e50, sqrt(1e100));
126 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56, 128 checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56,
127 Math.sqrt(12345678901234e99)); 129 sqrt(12345678901234e99));
128 } 130 }
129 131
130 static void testExp() { 132 static void testExp() {
131 checkVeryClose(Math.E, Math.exp(1.0)); 133 checkVeryClose(E, exp(1.0));
132 final EPSILON = 1e-15; 134 final EPSILON = 1e-15;
133 checkClose(10.0, Math.exp(Math.LN10), EPSILON); 135 checkClose(10.0, exp(LN10), EPSILON);
134 checkClose(2.0, Math.exp(Math.LN2), EPSILON); 136 checkClose(2.0, exp(LN2), EPSILON);
135 } 137 }
136 138
137 static void testLog() { 139 static void testLog() {
138 // Even though E is imprecise, it is good enough to get really close to 1. 140 // Even though E is imprecise, it is good enough to get really close to 1.
139 // We still provide an epsilon. 141 // We still provide an epsilon.
140 checkClose(1.0, Math.log(Math.E), 1e-16); 142 checkClose(1.0, log(E), 1e-16);
141 checkVeryClose(Math.LN10, Math.log(10.0)); 143 checkVeryClose(LN10, log(10.0));
142 checkVeryClose(Math.LN2, Math.log(2.0)); 144 checkVeryClose(LN2, log(2.0));
143 } 145 }
144 146
145 static void testPow() { 147 static void testPow() {
146 checkVeryClose(16.0, Math.pow(4.0, 2.0)); 148 checkVeryClose(16.0, pow(4.0, 2.0));
147 checkVeryClose(Math.SQRT2, Math.pow(2.0, 0.5)); 149 checkVeryClose(SQRT2, pow(2.0, 0.5));
148 checkVeryClose(Math.SQRT1_2, Math.pow(0.5, 0.5)); 150 checkVeryClose(SQRT1_2, pow(0.5, 0.5));
149 } 151 }
150 152
151 static bool parseIntThrowsBadNumberFormatException(str) { 153 static bool parseIntThrowsBadNumberFormatException(str) {
152 try { 154 try {
153 Math.parseInt(str); 155 parseInt(str);
154 return false; 156 return false;
155 } catch (BadNumberFormatException e) { 157 } catch (BadNumberFormatException e) {
156 return true; 158 return true;
157 } 159 }
158 } 160 }
159 161
160 static void testParseInt() { 162 static void testParseInt() {
161 Expect.equals(499, Math.parseInt("499")); 163 Expect.equals(499, parseInt("499"));
162 Expect.equals(499, Math.parseInt("+499")); 164 Expect.equals(499, parseInt("+499"));
163 Expect.equals(-499, Math.parseInt("-499")); 165 Expect.equals(-499, parseInt("-499"));
164 Expect.equals(499, Math.parseInt(" 499 ")); 166 Expect.equals(499, parseInt(" 499 "));
165 Expect.equals(499, Math.parseInt(" +499 ")); 167 Expect.equals(499, parseInt(" +499 "));
166 Expect.equals(-499, Math.parseInt(" -499 ")); 168 Expect.equals(-499, parseInt(" -499 "));
167 Expect.equals(0, Math.parseInt("0")); 169 Expect.equals(0, parseInt("0"));
168 Expect.equals(0, Math.parseInt("+0")); 170 Expect.equals(0, parseInt("+0"));
169 Expect.equals(0, Math.parseInt("-0")); 171 Expect.equals(0, parseInt("-0"));
170 Expect.equals(0, Math.parseInt(" 0 ")); 172 Expect.equals(0, parseInt(" 0 "));
171 Expect.equals(0, Math.parseInt(" +0 ")); 173 Expect.equals(0, parseInt(" +0 "));
172 Expect.equals(0, Math.parseInt(" -0 ")); 174 Expect.equals(0, parseInt(" -0 "));
173 Expect.equals(0x1234567890, Math.parseInt("0x1234567890")); 175 Expect.equals(0x1234567890, parseInt("0x1234567890"));
174 Expect.equals(-0x1234567890, Math.parseInt("-0x1234567890")); 176 Expect.equals(-0x1234567890, parseInt("-0x1234567890"));
175 Expect.equals(0x1234567890, Math.parseInt(" 0x1234567890 ")); 177 Expect.equals(0x1234567890, parseInt(" 0x1234567890 "));
176 Expect.equals(-0x1234567890, Math.parseInt(" -0x1234567890 ")); 178 Expect.equals(-0x1234567890, parseInt(" -0x1234567890 "));
177 Expect.equals(256, Math.parseInt("0x100")); 179 Expect.equals(256, parseInt("0x100"));
178 Expect.equals(-256, Math.parseInt("-0x100")); 180 Expect.equals(-256, parseInt("-0x100"));
179 Expect.equals(256, Math.parseInt(" 0x100 ")); 181 Expect.equals(256, parseInt(" 0x100 "));
180 Expect.equals(-256, Math.parseInt(" -0x100 ")); 182 Expect.equals(-256, parseInt(" -0x100 "));
181 Expect.equals(0xabcdef, Math.parseInt("0xabcdef")); 183 Expect.equals(0xabcdef, parseInt("0xabcdef"));
182 Expect.equals(0xABCDEF, Math.parseInt("0xABCDEF")); 184 Expect.equals(0xABCDEF, parseInt("0xABCDEF"));
183 Expect.equals(0xabcdef, Math.parseInt("0xabCDEf")); 185 Expect.equals(0xabcdef, parseInt("0xabCDEf"));
184 Expect.equals(-0xabcdef, Math.parseInt("-0xabcdef")); 186 Expect.equals(-0xabcdef, parseInt("-0xabcdef"));
185 Expect.equals(-0xABCDEF, Math.parseInt("-0xABCDEF")); 187 Expect.equals(-0xABCDEF, parseInt("-0xABCDEF"));
186 Expect.equals(0xabcdef, Math.parseInt(" 0xabcdef ")); 188 Expect.equals(0xabcdef, parseInt(" 0xabcdef "));
187 Expect.equals(0xABCDEF, Math.parseInt(" 0xABCDEF ")); 189 Expect.equals(0xABCDEF, parseInt(" 0xABCDEF "));
188 Expect.equals(-0xabcdef, Math.parseInt(" -0xabcdef ")); 190 Expect.equals(-0xabcdef, parseInt(" -0xabcdef "));
189 Expect.equals(-0xABCDEF, Math.parseInt(" -0xABCDEF ")); 191 Expect.equals(-0xABCDEF, parseInt(" -0xABCDEF "));
190 Expect.equals(0xabcdef, Math.parseInt("0x00000abcdef")); 192 Expect.equals(0xabcdef, parseInt("0x00000abcdef"));
191 Expect.equals(0xABCDEF, Math.parseInt("0x00000ABCDEF")); 193 Expect.equals(0xABCDEF, parseInt("0x00000ABCDEF"));
192 Expect.equals(-0xabcdef, Math.parseInt("-0x00000abcdef")); 194 Expect.equals(-0xabcdef, parseInt("-0x00000abcdef"));
193 Expect.equals(-0xABCDEF, Math.parseInt("-0x00000ABCDEF")); 195 Expect.equals(-0xABCDEF, parseInt("-0x00000ABCDEF"));
194 Expect.equals(0xabcdef, Math.parseInt(" 0x00000abcdef ")); 196 Expect.equals(0xabcdef, parseInt(" 0x00000abcdef "));
195 Expect.equals(0xABCDEF, Math.parseInt(" 0x00000ABCDEF ")); 197 Expect.equals(0xABCDEF, parseInt(" 0x00000ABCDEF "));
196 Expect.equals(-0xabcdef, Math.parseInt(" -0x00000abcdef ")); 198 Expect.equals(-0xabcdef, parseInt(" -0x00000abcdef "));
197 Expect.equals(-0xABCDEF, Math.parseInt(" -0x00000ABCDEF ")); 199 Expect.equals(-0xABCDEF, parseInt(" -0x00000ABCDEF "));
198 Expect.equals(10, Math.parseInt("010")); 200 Expect.equals(10, parseInt("010"));
199 Expect.equals(-10, Math.parseInt("-010")); 201 Expect.equals(-10, parseInt("-010"));
200 Expect.equals(10, Math.parseInt(" 010 ")); 202 Expect.equals(10, parseInt(" 010 "));
201 Expect.equals(-10, Math.parseInt(" -010 ")); 203 Expect.equals(-10, parseInt(" -010 "));
202 Expect.equals(9, Math.parseInt("09")); 204 Expect.equals(9, parseInt("09"));
203 Expect.equals(9, Math.parseInt(" 09 ")); 205 Expect.equals(9, parseInt(" 09 "));
204 Expect.equals(-9, Math.parseInt("-09")); 206 Expect.equals(-9, parseInt("-09"));
205 Expect.equals(true, parseIntThrowsBadNumberFormatException("1b")); 207 Expect.equals(true, parseIntThrowsBadNumberFormatException("1b"));
206 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1b ")); 208 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1b "));
207 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1 b ")); 209 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1 b "));
208 Expect.equals(true, parseIntThrowsBadNumberFormatException("1e2")); 210 Expect.equals(true, parseIntThrowsBadNumberFormatException("1e2"));
209 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1e2 ")); 211 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1e2 "));
210 Expect.equals(true, parseIntThrowsBadNumberFormatException("00x12")); 212 Expect.equals(true, parseIntThrowsBadNumberFormatException("00x12"));
211 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 00x12 ")); 213 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 00x12 "));
212 Expect.equals(true, parseIntThrowsBadNumberFormatException("-1b")); 214 Expect.equals(true, parseIntThrowsBadNumberFormatException("-1b"));
213 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1b ")); 215 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1b "));
214 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1 b ")); 216 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1 b "));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 testLog(); 250 testLog();
249 testExp(); 251 testExp();
250 testPow(); 252 testPow();
251 testParseInt(); 253 testParseInt();
252 } 254 }
253 } 255 }
254 256
255 main() { 257 main() {
256 MathTest.testMain(); 258 MathTest.testMain();
257 } 259 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698