1"use math"; 2"use strict"; 3 4function assert(actual, expected, message) { 5 if (arguments.length == 1) 6 expected = true; 7 8 if (actual === expected) 9 return; 10 11 if (actual !== null && expected !== null 12 && typeof actual == 'object' && typeof expected == 'object' 13 && actual.toString() === expected.toString()) 14 return; 15 16 throw Error("assertion failed: got |" + actual + "|" + 17 ", expected |" + expected + "|" + 18 (message ? " (" + message + ")" : "")); 19} 20 21function assertThrows(err, func) 22{ 23 var ex; 24 ex = false; 25 try { 26 func(); 27 } catch(e) { 28 ex = true; 29 assert(e instanceof err); 30 } 31 assert(ex, true, "exception expected"); 32} 33 34// load more elaborate version of assert if available 35try { __loadScript("test_assert.js"); } catch(e) {} 36 37/*----------------*/ 38 39function pow(a, n) 40{ 41 var r, i; 42 r = 1; 43 for(i = 0; i < n; i++) 44 r *= a; 45 return r; 46} 47 48function test_integer() 49{ 50 var a, r; 51 a = pow(3, 100); 52 assert((a - 1) != a); 53 assert(a == 515377520732011331036461129765621272702107522001); 54 assert(a == 0x5a4653ca673768565b41f775d6947d55cf3813d1); 55 assert(Integer.isInteger(1) === true); 56 assert(Integer.isInteger(1.0) === false); 57 58 assert(Integer.floorLog2(0) === -1); 59 assert(Integer.floorLog2(7) === 2); 60 61 r = 1 << 31; 62 assert(r, 2147483648, "1 << 31 === 2147483648"); 63 64 r = 1 << 32; 65 assert(r, 4294967296, "1 << 32 === 4294967296"); 66 67 r = (1 << 31) < 0; 68 assert(r, false, "(1 << 31) < 0 === false"); 69 70 assert(typeof 1 === "number"); 71 assert(typeof 9007199254740991 === "number"); 72 assert(typeof 9007199254740992 === "bigint"); 73} 74 75function test_float() 76{ 77 assert(typeof 1.0 === "bigfloat"); 78 assert(1 == 1.0); 79 assert(1 !== 1.0); 80} 81 82/* jscalc tests */ 83 84function test_modulo() 85{ 86 var i, p, a, b; 87 88 /* Euclidian modulo operator */ 89 assert((-3) % 2 == 1); 90 assert(3 % (-2) == 1); 91 92 p = 101; 93 for(i = 1; i < p; i++) { 94 a = Integer.invmod(i, p); 95 assert(a >= 0 && a < p); 96 assert((i * a) % p == 1); 97 } 98 99 assert(Integer.isPrime(2^107-1)); 100 assert(!Integer.isPrime((2^107-1) * (2^89-1))); 101 a = Integer.factor((2^89-1)*2^3*11*13^2*1009); 102 assert(a == [ 2,2,2,11,13,13,1009,618970019642690137449562111 ]); 103} 104 105function test_fraction() 106{ 107 assert((1/3 + 1).toString(), "4/3") 108 assert((2/3)^30, 1073741824/205891132094649); 109 assert(1/3 < 2/3); 110 assert(1/3 < 1); 111 assert(1/3 == 1.0/3); 112 assert(1.0/3 < 2/3); 113} 114 115function test_mod() 116{ 117 var a, b, p; 118 119 a = Mod(3, 101); 120 b = Mod(-1, 101); 121 assert((a + b) == Mod(2, 101)); 122 assert(a ^ 100 == Mod(1, 101)); 123 124 p = 2 ^ 607 - 1; /* mersenne prime */ 125 a = Mod(3, p) ^ (p - 1); 126 assert(a == Mod(1, p)); 127} 128 129function test_polynomial() 130{ 131 var a, b, q, r, t, i; 132 a = (1 + X) ^ 4; 133 assert(a == X^4+4*X^3+6*X^2+4*X+1); 134 135 r = (1 + X); 136 q = (1+X+X^2); 137 b = (1 - X^2); 138 a = q * b + r; 139 t = Polynomial.divrem(a, b); 140 assert(t[0] == q); 141 assert(t[1] == r); 142 143 a = 1 + 2*X + 3*X^2; 144 assert(a.apply(0.1) == 1.23); 145 146 a = 1-2*X^2+2*X^3; 147 assert(deriv(a) == (6*X^2-4*X)); 148 assert(deriv(integ(a)) == a); 149 150 a = (X-1)*(X-2)*(X-3)*(X-4)*(X-0.1); 151 r = polroots(a); 152 for(i = 0; i < r.length; i++) { 153 b = abs(a.apply(r[i])); 154 assert(b <= 1e-13); 155 } 156} 157 158function test_poly_mod() 159{ 160 var a, p; 161 162 /* modulo using polynomials */ 163 p = X^2 + X + 1; 164 a = PolyMod(3+X, p) ^ 10; 165 assert(a == PolyMod(-3725*X-18357, p)); 166 167 a = PolyMod(1/X, 1+X^2); 168 assert(a == PolyMod(-X, X^2+1)); 169} 170 171function test_rfunc() 172{ 173 var a; 174 a = (X+1)/((X+1)*(X-1)); 175 assert(a == 1/(X-1)); 176 a = (X + 2) / (X - 2); 177 assert(a.apply(1/3) == -7/5); 178 179 assert(deriv((X^2-X+1)/(X-1)) == (X^2-2*X)/(X^2-2*X+1)); 180} 181 182function test_series() 183{ 184 var a, b; 185 a = 1+X+O(X^5); 186 b = a.inverse(); 187 assert(b == 1-X+X^2-X^3+X^4+O(X^5)); 188 assert(deriv(b) == -1+2*X-3*X^2+4*X^3+O(X^4)); 189 assert(deriv(integ(b)) == b); 190 191 a = Series(1/(1-X), 5); 192 assert(a == 1+X+X^2+X^3+X^4+O(X^5)); 193 b = a.apply(0.1); 194 assert(b == 1.1111); 195 196 assert(exp(3*X^2+O(X^10)) == 1+3*X^2+9/2*X^4+9/2*X^6+27/8*X^8+O(X^10)); 197 assert(sin(X+O(X^6)) == X-1/6*X^3+1/120*X^5+O(X^6)); 198 assert(cos(X+O(X^6)) == 1-1/2*X^2+1/24*X^4+O(X^6)); 199 assert(tan(X+O(X^8)) == X+1/3*X^3+2/15*X^5+17/315*X^7+O(X^8)); 200 assert((1+X+O(X^6))^(2+X) == 1+2*X+2*X^2+3/2*X^3+5/6*X^4+5/12*X^5+O(X^6)); 201} 202 203function test_matrix() 204{ 205 var a, b, r; 206 a = [[1, 2],[3, 4]]; 207 b = [3, 4]; 208 r = a * b; 209 assert(r == [11, 25]); 210 r = (a^-1) * 2; 211 assert(r == [[-4, 2],[3, -1]]); 212 213 assert(norm2([1,2,3]) == 14); 214 215 assert(diag([1,2,3]) == [ [ 1, 0, 0 ], [ 0, 2, 0 ], [ 0, 0, 3 ] ]); 216 assert(trans(a) == [ [ 1, 3 ], [ 2, 4 ] ]); 217 assert(trans([1,2,3]) == [[1,2,3]]); 218 assert(trace(a) == 5); 219 220 assert(charpoly(Matrix.hilbert(4)) == X^4-176/105*X^3+3341/12600*X^2-41/23625*X+1/6048000); 221 assert(det(Matrix.hilbert(4)) == 1/6048000); 222 223 a = [[1,2,1],[-2,-3,1],[3,5,0]]; 224 assert(rank(a) == 2); 225 assert(ker(a) == [ [ 5 ], [ -3 ], [ 1 ] ]); 226 227 assert(dp([1, 2, 3], [3, -4, -7]) === -26); 228 assert(cp([1, 2, 3], [3, -4, -7]) == [ -2, 16, -10 ]); 229} 230 231function assert_eq(a, ref) 232{ 233 assert(abs(a / ref - 1.0) <= 1e-15); 234} 235 236function test_trig() 237{ 238 assert_eq(sin(1/2), 0.479425538604203); 239 assert_eq(sin(2+3*I), 9.154499146911428-4.168906959966565*I); 240 assert_eq(cos(2+3*I), -4.189625690968807-9.109227893755337*I); 241 assert_eq((2+0.5*I)^(1.1-0.5*I), 2.494363021357619-0.23076804554558092*I); 242 assert_eq(sqrt(2*I), 1 + I); 243} 244 245test_integer(); 246test_float(); 247 248test_modulo(); 249test_fraction(); 250test_mod(); 251test_polynomial(); 252test_poly_mod(); 253test_rfunc(); 254test_series(); 255test_matrix(); 256test_trig(); 257