|
| 1 | +open RescriptCore |
| 2 | + |
| 3 | +let eq = (a, b) => a == b |
| 4 | + |
| 5 | +// ===== BigInt64Array Tests ===== |
| 6 | + |
| 7 | +Test.run(__POS_OF__("bytes per element is 8"), BigInt64Array.Constants.bytesPerElement, eq, 8) |
| 8 | + |
| 9 | +let num1 = BigInt.fromString("123456789") |
| 10 | +let num2 = BigInt.fromString("987654321") |
| 11 | +let num3 = BigInt.fromString("555555555") |
| 12 | + |
| 13 | +let assertTrue = (message, predicate) => { |
| 14 | + try { |
| 15 | + if !predicate() { |
| 16 | + message->Error.make->Error.raise |
| 17 | + } |
| 18 | + } catch { |
| 19 | + | _ => message->Error.make->Error.raise |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +let assertWillThrow = (message, f) => { |
| 24 | + let didThrow = ref(false) |
| 25 | + try { |
| 26 | + f() |
| 27 | + } catch { |
| 28 | + | _ => didThrow := true |
| 29 | + } |
| 30 | + if didThrow.contents == false { |
| 31 | + message->Error.make->Error.raise |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +let areSame = (x: BigInt.t, y: BigInt.t) => BigInt.toString(x) == BigInt.toString(y) |
| 36 | + |
| 37 | +// What's going on here? |
| 38 | +// assertTrue("big ints if different then not equal", () => num1 != num2) |
| 39 | +// assertTrue("big ints if same then equal", () => num1 == num1) |
| 40 | + |
| 41 | +assertTrue("fromArray", () => |
| 42 | + [num1, num2]->BigInt64Array.fromArray->TypedArray.get(1)->Option.getExn->areSame(num2) |
| 43 | +) |
| 44 | + |
| 45 | +assertTrue("fromBuffer", () => { |
| 46 | + let x = ArrayBuffer.make(16)->BigInt64Array.fromBuffer |
| 47 | + x->TypedArray.set(1, num2) |
| 48 | + x->TypedArray.get(1)->Option.getExn->areSame(num2) |
| 49 | +}) |
| 50 | + |
| 51 | +assertWillThrow("fromBuffer when too short can throw when used", () => { |
| 52 | + let x = ArrayBuffer.make(1)->BigInt64Array.fromBuffer |
| 53 | + x->TypedArray.set(0, num1) |
| 54 | + x->TypedArray.get(0)->Option.getExn->areSame(num1)->ignore |
| 55 | +}) |
| 56 | + |
| 57 | +assertTrue("fromBufferWithRange", () => { |
| 58 | + let x = ArrayBuffer.make(16)->BigInt64Array.fromBufferWithRange(~byteOffset=0, ~length=1) |
| 59 | + x->TypedArray.set(0, num1) |
| 60 | + x->TypedArray.get(0)->Option.getExn->areSame(num1) |
| 61 | +}) |
| 62 | + |
| 63 | +// assertWillThrow("testing", () => {"a"->Error.make->Error.raise}) |
| 64 | +// assertWillThrow("testing", () => {Console.log("f")}) |
| 65 | + |
| 66 | +assertWillThrow("fromBufferWithRange is unsafe, out of range", () => { |
| 67 | + let x = ArrayBuffer.make(16)->BigInt64Array.fromBufferWithRange(~byteOffset=13, ~length=1) |
| 68 | + x->TypedArray.set(0, num1) |
| 69 | + x->TypedArray.get(0)->Option.getExn->areSame(num1)->ignore |
| 70 | +}) |
| 71 | + |
| 72 | +assertTrue("fromLength is NOT in bytes", () => { |
| 73 | + let x = BigInt64Array.fromLength(1) |
| 74 | + TypedArray.byteLength(x) == 8 |
| 75 | +}) |
| 76 | +let o = BigInt64Array.fromArrayLikeOrIterableWithMap |
0 commit comments