1"use strict"; 2 3function assert(actual, expected, message) { 4 if (arguments.length == 1) 5 expected = true; 6 7 if (actual === expected) 8 return; 9 10 if (actual !== null && expected !== null 11 && typeof actual == 'object' && typeof expected == 'object' 12 && actual.toString() === expected.toString()) 13 return; 14 15 throw Error("assertion failed: got |" + actual + "|" + 16 ", expected |" + expected + "|" + 17 (message ? " (" + message + ")" : "")); 18} 19 20/* operators overloading with Operators.create() */ 21function test_operators_create() { 22 class Vec2 23 { 24 constructor(x, y) { 25 this.x = x; 26 this.y = y; 27 } 28 static mul_scalar(p1, a) { 29 var r = new Vec2(); 30 r.x = p1.x * a; 31 r.y = p1.y * a; 32 return r; 33 } 34 toString() { 35 return "Vec2(" + this.x + "," + this.y + ")"; 36 } 37 } 38 39 Vec2.prototype[Symbol.operatorSet] = Operators.create( 40 { 41 "+"(p1, p2) { 42 var r = new Vec2(); 43 r.x = p1.x + p2.x; 44 r.y = p1.y + p2.y; 45 return r; 46 }, 47 "-"(p1, p2) { 48 var r = new Vec2(); 49 r.x = p1.x - p2.x; 50 r.y = p1.y - p2.y; 51 return r; 52 }, 53 "=="(a, b) { 54 return a.x == b.x && a.y == b.y; 55 }, 56 "<"(a, b) { 57 var r; 58 /* lexicographic order */ 59 if (a.x == b.x) 60 r = (a.y < b.y); 61 else 62 r = (a.x < b.x); 63 return r; 64 }, 65 "++"(a) { 66 var r = new Vec2(); 67 r.x = a.x + 1; 68 r.y = a.y + 1; 69 return r; 70 } 71 }, 72 { 73 left: Number, 74 "*"(a, b) { 75 return Vec2.mul_scalar(b, a); 76 } 77 }, 78 { 79 right: Number, 80 "*"(a, b) { 81 return Vec2.mul_scalar(a, b); 82 } 83 }); 84 85 var a = new Vec2(1, 2); 86 var b = new Vec2(3, 4); 87 var r; 88 89 r = a * 2 + 3 * b; 90 assert(r.x === 11 && r.y === 16); 91 assert(a == a, true); 92 assert(a == b, false); 93 assert(a != a, false); 94 assert(a < b, true); 95 assert(a <= b, true); 96 assert(b < a, false); 97 assert(b <= a, false); 98 assert(a <= a, true); 99 assert(a >= a, true); 100 a++; 101 assert(a.x === 2 && a.y === 3); 102 r = ++a; 103 assert(a.x === 3 && a.y === 4); 104 assert(r === a); 105} 106 107/* operators overloading thru inheritance */ 108function test_operators() 109{ 110 var Vec2; 111 112 function mul_scalar(p1, a) { 113 var r = new Vec2(); 114 r.x = p1.x * a; 115 r.y = p1.y * a; 116 return r; 117 } 118 119 var vec2_ops = Operators({ 120 "+"(p1, p2) { 121 var r = new Vec2(); 122 r.x = p1.x + p2.x; 123 r.y = p1.y + p2.y; 124 return r; 125 }, 126 "-"(p1, p2) { 127 var r = new Vec2(); 128 r.x = p1.x - p2.x; 129 r.y = p1.y - p2.y; 130 return r; 131 }, 132 "=="(a, b) { 133 return a.x == b.x && a.y == b.y; 134 }, 135 "<"(a, b) { 136 var r; 137 /* lexicographic order */ 138 if (a.x == b.x) 139 r = (a.y < b.y); 140 else 141 r = (a.x < b.x); 142 return r; 143 }, 144 "++"(a) { 145 var r = new Vec2(); 146 r.x = a.x + 1; 147 r.y = a.y + 1; 148 return r; 149 } 150 }, 151 { 152 left: Number, 153 "*"(a, b) { 154 return mul_scalar(b, a); 155 } 156 }, 157 { 158 right: Number, 159 "*"(a, b) { 160 return mul_scalar(a, b); 161 } 162 }); 163 164 Vec2 = class Vec2 extends vec2_ops 165 { 166 constructor(x, y) { 167 super(); 168 this.x = x; 169 this.y = y; 170 } 171 toString() { 172 return "Vec2(" + this.x + "," + this.y + ")"; 173 } 174 } 175 176 var a = new Vec2(1, 2); 177 var b = new Vec2(3, 4); 178 var r; 179 180 r = a * 2 + 3 * b; 181 assert(r.x === 11 && r.y === 16); 182 assert(a == a, true); 183 assert(a == b, false); 184 assert(a != a, false); 185 assert(a < b, true); 186 assert(a <= b, true); 187 assert(b < a, false); 188 assert(b <= a, false); 189 assert(a <= a, true); 190 assert(a >= a, true); 191 a++; 192 assert(a.x === 2 && a.y === 3); 193 r = ++a; 194 assert(a.x === 3 && a.y === 4); 195 assert(r === a); 196} 197 198function test_default_op() 199{ 200 assert(Object(1) + 2, 3); 201 assert(Object(1) + true, 2); 202 assert(-Object(1), -1); 203} 204 205test_operators_create(); 206test_operators(); 207test_default_op(); 208