Skip to content

Commit

Permalink
test: Expand test coverage of primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Nov 21, 2024
1 parent fadeecc commit 991547c
Show file tree
Hide file tree
Showing 30 changed files with 3,185 additions and 441 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dev:examples": "tsx src/cli.ts build examples --output-awst --output-awst-json",
"dev:approvals": "rimraf tests/approvals/out && tsx src/cli.ts build tests/approvals --output-awst --output-awst-json --output-ssa-ir --out-dir out/[name] --optimization-level 0",
"dev:expected-output": "tsx src/cli.ts build tests/expected-output --dry-run",
"dev:testing": "tsx src/cli.ts build tests/approvals/accounts.algo.ts --output-awst --output-awst-json --output-ssa-ir --log-level=info --log-level info --out-dir out/[name]",
"dev:testing": "tsx src/cli.ts build tests/approvals/biguint-expressions.algo.ts --output-awst --output-awst-json --output-ssa-ir --log-level=info --log-level info --out-dir out/[name]",
"audit": "better-npm-audit audit",
"format": "prettier --write .",
"lint": "eslint \"src/**/*.ts\"",
Expand Down
2 changes: 1 addition & 1 deletion src/awst/wtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export namespace wtypes {

toString(): string {
if (this.names) {
return `${this.name ?? ''}{ ${this.names.map((n, i) => `${n}: ${this.types[i]}`).join(', ')} }`
return `${this.name === 'Anonymous' ? '' : this.name}{ ${this.names.map((n, i) => `${n}: ${this.types[i]}`).join(', ')} }`
}
return `${this.immutable ? 'readonly' : ''}${this.name ?? ''}[${this.types.join(', ')}]`
}
Expand Down
18 changes: 14 additions & 4 deletions tests/approvals/biguint-expressions.algo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import { BaseContract, BigUint } from '@algorandfoundation/algorand-typescript'
import type { biguint, bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import { assert, BaseContract, BigUint, Bytes, op } from '@algorandfoundation/algorand-typescript'
import { itob } from '@algorandfoundation/algorand-typescript/op'

function test(a: uint64, b: boolean, c: bytes) {
function testConstructor(a: uint64, b: boolean, c: bytes) {
BigUint()
BigUint(true)
BigUint(false)
Expand All @@ -18,9 +18,19 @@ function test(a: uint64, b: boolean, c: bytes) {
BigUint(c)
}

function testOps(smaller: biguint, larger: biguint) {
assert(smaller < larger)
assert(smaller <= smaller)
assert(larger > smaller)
assert(larger >= larger)
assert(smaller === BigUint(smaller))
assert(smaller === BigUint(op.bzero(4).concat(Bytes(smaller))), 'Leading zeros should be ignored in equality')
}

class DemoContract extends BaseContract {
public approvalProgram() {
test(1, false, itob(4))
testConstructor(1, false, itob(4))
testOps(500n, 1000n)
return true
}
}
17 changes: 12 additions & 5 deletions tests/approvals/byte-expressions.algo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import type { biguint, uint64 } from '@algorandfoundation/algorand-typescript'
import { assert, BigUint, Bytes, op, Uint64 } from '@algorandfoundation/algorand-typescript'
import { assert, BaseContract, BigUint, Bytes, op, Uint64 } from '@algorandfoundation/algorand-typescript'

function test(a: uint64, b: biguint, c: string) {
assert(Bytes().length === 0)
assert(Bytes(a) === op.itob(a))
assert(Bytes(Uint64(1)) === Bytes.fromHex('0000000000000001'))
assert(Bytes().length === 0, 'Empty bytes has length of 0')
assert(Bytes(a) === op.itob(a), 'Bytes(uint64) is equivalent to itob')
assert(Bytes(Uint64(1)) === Bytes.fromHex('0000000000000001'), 'Bytes(uint64) returns an 8 byte encoding')
assert(Bytes(BigUint(256)) === Bytes.fromHex('0100'))
assert(BigUint(Bytes(b)) === b)
assert(BigUint(Bytes(b)) === b, 'Round trip of biguint to bytes and back results in the same value')
assert(String(Bytes(c)) === c)
assert(Bytes(Bytes('123')) === Bytes('123'))
assert(Bytes([1, 2, 3, 4]) === Bytes.fromHex('01020304'))
}

class DemoContract extends BaseContract {
public approvalProgram() {
test(1, 50n, 'things')
return true
}
}
2 changes: 1 addition & 1 deletion tests/approvals/out/accounts/accounts.awst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract AccountsContract
return True
}

getAccountInfo(): Anonymous{ bytes: bytes, balance: uint64, minBalance: uint64, authAddress: bytes, totalNumUint: uint64, totalNumByteSlice: uint64, totalExtraAppPages: uint64, totalAppsCreated: uint64, totalAppsOptedIn: uint64, totalAssetsCreated: uint64, totalAssets: uint64, totalBoxes: uint64, totalBoxBytes: uint64, isOptInApp: bool, isOptInAsset: bool }
getAccountInfo(): { bytes: bytes, balance: uint64, minBalance: uint64, authAddress: bytes, totalNumUint: uint64, totalNumByteSlice: uint64, totalExtraAppPages: uint64, totalAppsCreated: uint64, totalAppsOptedIn: uint64, totalAssetsCreated: uint64, totalAssets: uint64, totalBoxes: uint64, totalBoxBytes: uint64, isOptInApp: bool, isOptInAsset: bool }
{
return { bytes: reinterpret_cast<bytes>(account), balance: checked_maybe(acct_params_get<AcctBalance>(account), comment=account funded), minBalance: checked_maybe(acct_params_get<AcctMinBalance>(account), comment=account funded), authAddress: reinterpret_cast<bytes>(checked_maybe(acct_params_get<AcctAuthAddr>(account), comment=account funded)), totalNumUint: checked_maybe(acct_params_get<AcctTotalNumUint>(account), comment=account funded), totalNumByteSlice: checked_maybe(acct_params_get<AcctTotalNumByteSlice>(account), comment=account funded), totalExtraAppPages: checked_maybe(acct_params_get<AcctTotalExtraAppPages>(account), comment=account funded), totalAppsCreated: checked_maybe(acct_params_get<AcctTotalAppsCreated>(account), comment=account funded), totalAppsOptedIn: checked_maybe(acct_params_get<AcctTotalAppsOptedIn>(account), comment=account funded), totalAssetsCreated: checked_maybe(acct_params_get<AcctTotalAssetsCreated>(account), comment=account funded), totalAssets: checked_maybe(acct_params_get<AcctTotalAssets>(account), comment=account funded), totalBoxes: checked_maybe(acct_params_get<AcctTotalBoxes>(account), comment=account funded), totalBoxBytes: checked_maybe(acct_params_get<AcctTotalBoxBytes>(account), comment=account funded), isOptInApp: app_opted_in(account, global<CurrentApplicationID>()), isOptInAsset: asset_holding_get<AssetBalance>(account, asset).1 }
}
Expand Down
10 changes: 5 additions & 5 deletions tests/approvals/out/arc4-struct/arc4-struct.awst
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ contract StructDemo

testVectorCreationAndEquality(): void
{
v1: Vector = new Vector(x=(#0 = { x: 0, y: 0 }).x, y=#0.y)
v1: Vector = new Vector(x=(#14 = { x: 0, y: 0 }).x, y=#14.y)
log(reinterpret_cast<bytes>(v1.x))
log(reinterpret_cast<bytes>(v1.y))
v2: Vector = new Vector(x=(#2 = { x: (#1 = { y: 0, x: 0 }).x, y: #1.y }).x, y=#2.y)
v2: Vector = new Vector(x=(#16 = { x: (#15 = { y: 0, x: 0 }).x, y: #15.y }).x, y=#16.y)
assert(reinterpret_cast<bytes>(v1) == reinterpret_cast<bytes>(v2))
}

addVectors(): Vector
{
return new Vector(x=(#3 = { x: ARC4_ENCODE(ARC4_DECODE(v1.x) + ARC4_DECODE(v2.x), wtype=arc4.uint64), y: ARC4_ENCODE(ARC4_DECODE(v1.y) + ARC4_DECODE(v2.y), wtype=arc4.uint64) }).x, y=#3.y)
return new Vector(x=(#17 = { x: ARC4_ENCODE(ARC4_DECODE(v1.x) + ARC4_DECODE(v2.x), wtype=arc4.uint64), y: ARC4_ENCODE(ARC4_DECODE(v1.y) + ARC4_DECODE(v2.y), wtype=arc4.uint64) }).x, y=#17.y)
}

implicitCastingAndSpreading(): void
{
v2: Vector = new Vector(x=(#4 = ARC4_DECODE(v1)).x, y=#4.y)
v3: Vector = new Vector(x=(#5 = { x: v2.x, y: v2.y }).x, y=#5.y)
v2: Vector = new Vector(x=(#18 = ARC4_DECODE(v1)).x, y=#18.y)
v3: Vector = new Vector(x=(#19 = { x: v2.x, y: v2.y }).x, y=#19.y)
assert(reinterpret_cast<bytes>(v1) == reinterpret_cast<bytes>(v2))
assert(reinterpret_cast<bytes>(v3) == reinterpret_cast<bytes>(v1))
}
Expand Down
Loading

0 comments on commit 991547c

Please sign in to comment.