Operators.uts 12.3 KB
Newer Older
Y
yurj26 已提交
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
import { describe, test, expect, Result } from './tests.uts'

export function testOperators(): Result {
    return describe("Operators", () => {
        test('+', () => {
            expect('hello ' + 'everyone').toEqual("hello everyone");
            function add(a: number, b: number): number {
                return a + b
            }
            expect(add(0, 0)).toEqual(0);
            expect(add(2, 2)).toEqual(4);
            expect(add(-2, -3)).toEqual(-5);
            expect(add(100, -100)).toEqual(0);
            expect(add(100, -0.00001)).toEqual(99.99999);
            expect(add(100, 0.00001)).toEqual(100.00001);
            expect(add(123456789, 987654321)).toEqual(1111111110);
            expect(add(1000000, 2000000)).toEqual(3000000);
            expect(add(-1000000, -2000000)).toEqual(-3000000);
            // kotlin 最大值 9223372036854775807,最小值 -9223372036854775808
            // 超出边界报错 error: The value is out of range‌
            // swift 最大值 9223372036854775807,最小值 -9223372036854775808
            // 超出边界报错 error: integer literal 'xxx' overflows when stored into 'Int'
            // web 最大值 1.7976931348623157e+308,最小值-1.7976931348623157e+308
            // 超出边界返回 Infinity
            // todo 统一处理
            // const max = 9223372036854775807
            // expect(add(max, max)).toEqual();
        })
        test('+=', () => {
            let a: number = 2;
            a += 3;
            expect(a).toEqual(5);
            let b: string = 'hello';
            b += ' world';
            expect(b).toEqual("hello world");
        })
        test('=', () => {
            let x: number = 2;
            const y: number = 3;
            expect(x).toEqual(2);
            x = y + 1;
            expect(x).toEqual(4);
            x = x * y
            expect(x).toEqual(12);
        })
        test('&', () => {
            const a: number = 5;     // 00000000000000000000000000000101
            const b: number = 3;     // 00000000000000000000000000000011
            expect(a & b).toEqual(1);  // 00000000000000000000000000000001
        })
        test('&=', () => {
            let a = 5;      // 00000000000000000000000000000101
            a &= 3;         // 00000000000000000000000000000011
            expect(a).toEqual(1);  // 00000000000000000000000000000001
        })
        test('~', () => {
            const a: number = 5;     // 00000000000000000000000000000101
            const b: number = -3;    // 11111111111111111111111111111101
            expect(~a).toEqual(-6);  // 11111111111111111111111111111010
            expect(~b).toEqual(2);  // 00000000000000000000000000000010
        })
        test('|', () => {
            const a: number = 5;        // 00000000000000000000000000000101
            const b: number = 3;        // 00000000000000000000000000000011
            expect(a | b).toEqual(7);  // 00000000000000000000000000000111
        })
        test('|=', () => {
            let a = 5;      // 00000000000000000000000000000101
            a |= 3;         // 00000000000000000000000000000011
            expect(a).toEqual(7);  // 00000000000000000000000000000111
        })
        test('^', () => {
            const a: number = 5;        // 00000000000000000000000000000101
            const b: number = 3;        // 00000000000000000000000000000011
            expect(a ^ b).toEqual(6);  // 00000000000000000000000000000110
        })
        test('^=', () => {
            let a = 5;      // 00000000000000000000000000000101
            a ^= 3;         // 00000000000000000000000000000011
            expect(a).toEqual(6);  // 00000000000000000000000000000110
        })
        test('--', () => {
            let x = 3;
            const y = x--;
            expect(`x:${x}, y:${y}`).toEqual("x:2, y:3");
            let a = 3;
            const b = --a;
            expect(`a:${a}, b:${b}`).toEqual("a:2, b:2");
        })
        test('/', () => {
            function divide (a: number, b: number): number {
                return a / b
            }
            expect(divide(10, 5)).toEqual(2);
            expect(divide(-10, -5)).toEqual(2);
            expect(divide(10, -5)).toEqual(-2);
            expect(divide(-10, 5)).toEqual(-2);
            expect(divide(0.1, 0.3)).toEqual(0.33333333333333337);
            expect(divide(0.3, 0.1)).toEqual(2.9999999999999996);
            expect(divide(1.5, 0.5)).toEqual(3);
            expect(divide(3, 2)).toEqual(1.5);
            // 暂不支持字面量
            // expect(3 / 2).toEqual(1.5);
            expect(divide(10, 3)).toEqual(3.3333333333333335);
            expect(divide(3, 10)).toEqual(0.3);
            expect(divide(1, 3)).toEqual(0.3333333333333333);
            expect(divide(1000000, 2000000)).toEqual(0.5);
            // kotlin java.lang.ArithmeticException: divide by zero
            // swift 返回 inf
            // web 返回 Infinity
            // todo 统一处理
            // expect(divide(1, 0)).toEqual();
        })
        test('/=', () => {
            let a:Number = 3;
            a /= 2;
            expect(a).toEqual(1.5);
        })
        test('==', () => {
            expect(1 == 1).toEqual(true);
            expect('hello' == 'hello').toEqual(true);
        })
        test('>', () => {
            expect(5 > 3).toEqual(true);
            expect(3 > 3).toEqual(false);
            expect('ab' > 'aa').toEqual(true);
        })
        test('++', () => {
            let x = 3;
            const y = x++;
            expect(`x:${x}, y:${y}`).toEqual("x:4, y:3");
            let a = 3;
            const b = ++a;
            expect(`a:${a}, b:${b}`).toEqual("a:4, b:4");
        })
        test('!=', () => {
            expect(1 != 1).toEqual(false);
            expect('hello' != 'hello').toEqual(false);
        })
        test('<<', () => {
            const a: number = 5;         // 00000000000000000000000000000101
            const b: number = 2;         // 00000000000000000000000000000010
            expect(a << b).toEqual(20);  // 00000000000000000000000000010100
        })
        test('<<=', () => {
            let a = 5; // 00000000000000000000000000000101
            a <<= 2;   // 00000000000000000000000000010100
            expect(a).toEqual(20);  // 00000000000000000000000000010100
        })
        test('<', () => {
            expect(5 < 3).toEqual(false);
            expect(3 < 3).toEqual(false);
            expect('aa' < 'ab').toEqual(true);
        })
        test('<=', () => {
            expect(5 <= 3).toEqual(false);
            expect(3 <= 3).toEqual(true);
            expect('aa' <= 'ab').toEqual(true);
        })
        test('&&', () => {
            const a: number = 3;
            const b: number = -2;
            expect(a > 0 && b > 0).toEqual(false);
        })
        // test('&&=', () => {
        // 	let a:Int = 1;
        // 	let b = 0;
        // 	a &&= 2;
        // 	expect(a).toEqual(2);
        // 	b &&= 2;
        // 	expect(b).toEqual(0);
        // })
        test('!', () => {
            const a: number = 3;
            const b: number = -2;
            expect(!(a > 0 || b > 0)).toEqual(false);
        })
        // test('??=', () => {
        // 	const a = { duration: 50 };
        // 	a.duration ??= 10;
        // 	expect(a.duration).toEqual(50);
        // 	a.speed ??= 25;
        // 	expect(a.duration).toEqual(25);
        // })
        test('||', () => {
            const a: number = 3;
            const b: number = -2;
            expect(a > 0 || b > 0).toEqual(true);
        })
        // test('||=', () => {
        // 	const a = { duration: 50, title: '' };
        // 	a.duration ||= 10;
        // 	expect(a.duration).toEqual(50);
        // 	a.title ||= 'title is empty.';
        // 	expect(a.title).toEqual("title is empty");
        // })
        test('*', () => {
            function multiply(a: number, b: number): number {
                return a * b
            } 
            expect(multiply(10, 0)).toEqual(0);
            expect(multiply(-10, 0)).toEqual(0);
            expect(multiply(10, 1)).toEqual(10);
            expect(multiply(1, 10)).toEqual(10);
            expect(multiply(1, -10)).toEqual(-10);
            expect(multiply(2, 3)).toEqual(6);
            expect(multiply(0.2, 0.3)).toEqual(0.06);
            expect(multiply(1.5, 6)).toEqual(9);
            expect(multiply(1000, 0.001)).toEqual(1);
            expect(multiply(1000, -0.001)).toEqual(-1);
            expect(multiply(1000, -0.001)).toEqual(-1);
            expect(multiply(9999, 9999)).toEqual(99980001);
            expect(multiply(100000000, 10)).toEqual(1000000000);
        })
        test('*=', () => {
            let a = 2;
            a *= 3;
            expect(a).toEqual(6);
        })
        // test('??', () => {
        // 	const foo = null ?? 'default string';
        // 	expect(foo).toEqual("default string");
        // 	const baz = 0 ?? 42;
        // 	expect(baz).toEqual(0);
        // })
        // test('?.', () => {
        // 	const adventurer = {
        // 	  name: 'Alice',
        // 	  cat: {
        // 	    name: 'Dinah'
        // 	  }
        // 	};
        // 	const dogName = adventurer.cat?.name;
        // 	console.log(dogName);
        // 	expect(dogName).toEqual(undefined);
        // })
        test('%', () => {
            expect(13 % 5).toEqual(3);
            expect(-13 % 5).toEqual(-3);
            expect(4 % 2).toEqual(0);
            expect(-4 % 2).toEqual(-0);
        })
        test('%=', () => {
            let a = 3;
            a %= 2;
            expect(a).toEqual(1);
        })
        test('>>', () => {
            const a: number = 5;          //  00000000000000000000000000000101
            const b: number = 2;          //  00000000000000000000000000000010
            const c: number = -5;         //  11111111111111111111111111111011
            expect(a >> b).toEqual(1); // 00000000000000000000000000000001
            expect(c >> b).toEqual(-2); // 11111111111111111111111111111110
        })
        test('>>=', () => {
            let a = 5;      //  00000000000000000000000000000101
            a >>= 2;        //  00000000000000000000000000000001
            expect(a).toEqual(1);
            let b = -5;     //  11111111111111111111111111111011
            b >>= 2;        //  11111111111111111111111111111110
            expect(b).toEqual(-2);
        })
        test('===', () => {
            // swift里,基础类型暂不支持!==,===对比
            expect(1 == 1).toEqual(true);
            expect('hello' == 'hello').toEqual(true);
        })
        test('!==', () => {
            // swift里,基础类型暂不支持!==,===对比
            expect(1 != 1).toEqual(false);
            expect('hello' != 'hello').toEqual(false);
        })
        test('-', () => {
            function subtract(a: number, b: number): number {
                return a - b
            }
            expect(subtract(3, 3)).toEqual(0);
            expect(subtract(5, 3)).toEqual(2);
            expect(subtract(-5, -3)).toEqual(-2);
            expect(subtract(-5, 3)).toEqual(-8);
            expect(subtract(5, -3)).toEqual(8);
            expect(subtract(5, 0)).toEqual(5);
            expect(subtract(1.5, 0.5)).toEqual(1);
            expect(subtract(5.5, 2)).toEqual(3.5);
            expect(subtract(5.5, 2)).toEqual(3.5);
            expect(subtract(2.22, 1.11)).toEqual(1.11);
            expect(subtract(10000000000, 1)).toEqual(9999999999);
            expect(subtract(-10000000000, 1)).toEqual(-10000000001);
        })
        test('-=', () => {
            let a = 2;
            a -= 3
            expect(a).toEqual(-1);
        })
        test('>>>', () => {
            const a: number = 5;          //  00000000000000000000000000000101
            const b: number = 2;          //  00000000000000000000000000000010
            const c: number = -5;         //  11111111111111111111111111111011
            expect(a >>> b).toEqual(1); // 00000000000000000000000000000001
lizhongyi_'s avatar
lizhongyi_ 已提交
300
            // expect(c >>> b).toEqual(1073741822); // 00111111111111111111111111111110
Y
yurj26 已提交
301 302 303 304 305 306 307
        })
        test('>>>=', () => {
            let a = 5; //  00000000000000000000000000000101
            a >>>= 2;  //  00000000000000000000000000000001
            expect(a).toEqual(1);
            let b = -5; // -00000000000000000000000000000101
            b >>>= 2;   //  00111111111111111111111111111110
lizhongyi_'s avatar
lizhongyi_ 已提交
308
            // expect(b).toEqual(1073741822);
Y
yurj26 已提交
309 310 311 312
        })
    })
}