Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-K-Ivanov committed Jul 8, 2020
2 parents 339a008 + 03439b6 commit c1fdc5c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions assembly/Hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ export class Hash extends Array<u8> {
static fromU8a (input: u8[]): Hash {
return new Hash(input);
}

@inline @operator('==')
static eq(a: Hash, b: Hash): bool {
let areEqual = true;
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
areEqual = false;
break;
}
}
return areEqual;
}
}
5 changes: 5 additions & 0 deletions assembly/Int/CompactInt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ export class CompactInt implements Codec {
const decodedData = Bytes.decodeCompactInt(value);
return new CompactInt(decodedData.value);
}

@inline @operator('==')
static eq(a: CompactInt, b: CompactInt): bool {
return a.value == b.value;
}
}
14 changes: 14 additions & 0 deletions assembly/__tests__/Hash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ describe("Hash", () => {
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]);
expect<string>(hash.toString()).toStrictEqual('0x255000000000000000000000000000000255');
});

it("equals should work", () => {
let hash: Hash = new Hash([0xff]);
let hash2: Hash = new Hash([0xff]);

assert(hash == hash2, "hashes are not equal")
})

it("should not be equal when hashes are different", () => {
let hash1: Hash = new Hash([0x01]);
let hash2: Hash = new Hash([0xff]);

assert(hash1 != hash2, "hashes should be different");
})
});

0 comments on commit c1fdc5c

Please # to comment.