Skip to content

Commit c1fdc5c

Browse files
2 parents 339a008 + 03439b6 commit c1fdc5c

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

assembly/Hash.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ export class Hash extends Array<u8> {
6565
static fromU8a (input: u8[]): Hash {
6666
return new Hash(input);
6767
}
68+
69+
@inline @operator('==')
70+
static eq(a: Hash, b: Hash): bool {
71+
let areEqual = true;
72+
for (let i = 0; i < a.length; i++) {
73+
if (a[i] != b[i]) {
74+
areEqual = false;
75+
break;
76+
}
77+
}
78+
return areEqual;
79+
}
6880
}

assembly/Int/CompactInt.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ export class CompactInt implements Codec {
6666
const decodedData = Bytes.decodeCompactInt(value);
6767
return new CompactInt(decodedData.value);
6868
}
69+
70+
@inline @operator('==')
71+
static eq(a: CompactInt, b: CompactInt): bool {
72+
return a.value == b.value;
73+
}
6974
}

assembly/__tests__/Hash.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,18 @@ describe("Hash", () => {
7272
hash = new Hash([0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff]);
7373
expect<string>(hash.toString()).toStrictEqual('0x255000000000000000000000000000000255');
7474
});
75+
76+
it("equals should work", () => {
77+
let hash: Hash = new Hash([0xff]);
78+
let hash2: Hash = new Hash([0xff]);
79+
80+
assert(hash == hash2, "hashes are not equal")
81+
})
82+
83+
it("should not be equal when hashes are different", () => {
84+
let hash1: Hash = new Hash([0x01]);
85+
let hash2: Hash = new Hash([0xff]);
86+
87+
assert(hash1 != hash2, "hashes should be different");
88+
})
7589
});

0 commit comments

Comments
 (0)