Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukovec committed Apr 16, 2024
1 parent bbb9f39 commit fe24c19
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
34 changes: 17 additions & 17 deletions solarkraft/src/state/value.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface Value {}
interface Value { }

/**
* Any of the follwing:
Expand All @@ -14,10 +14,10 @@ interface Value {}
abstract class IntValue implements Value {
val: bigint
type: string

constructor(v: bigint) {
this.val = v
if (!this.isValid()) {
if (!this.isValid()) {
throw new RangeError(`${v} lies outside the ${this.type} range.`)
}
}
Expand All @@ -30,8 +30,8 @@ export class IntValue_u32 extends IntValue {
val: bigint
type = "u32"

isValid(): boolean {
return (0n <= this.val) && (this.val <= 2n**32n)
isValid(): boolean {
return (0n <= this.val) && (this.val <= 2n ** 32n)
}
}

Expand All @@ -40,8 +40,8 @@ export class IntValue_i32 extends IntValue {
val: bigint
type = "i32"

isValid(): boolean {
return (-(2n**31n) <= this.val) && (this.val < (2n**31n))
isValid(): boolean {
return (-(2n ** 31n) <= this.val) && (this.val < (2n ** 31n))
}
}

Expand All @@ -50,8 +50,8 @@ export class IntValue_u64 extends IntValue {
val: bigint
type = "u64"

isValid(): boolean {
return (0n <= this.val) && (this.val <= 2n**64n)
isValid(): boolean {
return (0n <= this.val) && (this.val <= 2n ** 64n)
}
}

Expand All @@ -60,8 +60,8 @@ export class IntValue_i64 extends IntValue {
val: bigint
type = "i64"

isValid(): boolean {
return (-(2n**63n) <= this.val) && (this.val < (2n**63n))
isValid(): boolean {
return (-(2n ** 63n) <= this.val) && (this.val < (2n ** 63n))
}
}

Expand All @@ -70,8 +70,8 @@ export class IntValue_u128 extends IntValue {
val: bigint
type = "u128"

isValid(): boolean {
return (0n <= this.val) && (this.val <= 2n**128n)
isValid(): boolean {
return (0n <= this.val) && (this.val <= 2n ** 128n)
}
}

Expand All @@ -80,8 +80,8 @@ export class IntValue_i128 extends IntValue {
val: bigint
type = "i128"

isValid(): boolean {
return (-(2n**127n) <= this.val) && (this.val < (2n**127n))
isValid(): boolean {
return (-(2n ** 127n) <= this.val) && (this.val < (2n ** 127n))
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ export class AddrValue implements Value {

constructor(v: string) {
this.val = v
if (!this.regex.test(v)) {
if (!this.regex.test(v)) {
throw new TypeError(`Address must be exactly 56 uppercase alphanumeric characters, found: ${v}.`)
}
}
Expand All @@ -134,7 +134,7 @@ export class ArrValue implements Value {

constructor(v: IntValue_u32[], l?: number) {
this.val = v
if (typeof(l) !== 'undefined' && v.length !== l){
if (typeof (l) !== 'undefined' && v.length !== l) {
throw new TypeError(`Array declared as fixed-length ${l}, but actual length is ${v.length}.`)
}
}
Expand Down
63 changes: 30 additions & 33 deletions solarkraft/test/state/value.test.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,74 @@
import { assert } from 'chai'
import { describe, it } from 'mocha'

import {IntValue_u32, IntValue_i32, IntValue_u64, IntValue_i64,IntValue_u128, IntValue_i128, SymValue, AddrValue} from '../../src/state/value.js'
import { IntValue_u32, IntValue_i32, IntValue_u64, IntValue_i64, IntValue_u128, IntValue_i128, SymValue, AddrValue } from '../../src/state/value.js'

describe('Integer tests', () => {
it('asserts 32-bit integer constructors respect bounds', () => {
assert.throws( () => {new IntValue_u32(-1n)}, RangeError )
assert.throws( () => {new IntValue_u32(2n**32n + 1n)}, RangeError )
assert.throws( () => {new IntValue_i32(-(2n**31n) - 1n)}, RangeError )
assert.throws( () => {new IntValue_i32(2n**31n)}, RangeError )
assert.throws(() => { new IntValue_u32(-1n) }, RangeError)
assert.throws(() => { new IntValue_u32(2n ** 32n + 1n) }, RangeError)
assert.throws(() => { new IntValue_i32(-(2n ** 31n) - 1n) }, RangeError)
assert.throws(() => { new IntValue_i32(2n ** 31n) }, RangeError)

const x_u32 = new IntValue_u32(2n**20n)
const x_u32 = new IntValue_u32(2n ** 20n)
assert(x_u32.isValid)
assert(x_u32.type === "u32")
assert(x_u32.val === 2n**20n)
assert(x_u32.val === 2n ** 20n)

const x_i32 = new IntValue_i32(-(2n**20n))
const x_i32 = new IntValue_i32(-(2n ** 20n))
assert(x_i32.isValid)
assert(x_i32.type === "i32")
assert(x_i32.val === -(2n**20n))
assert(x_i32.val === -(2n ** 20n))
})

it('asserts 64-bit integer constructors respect bounds', () => {
assert.throws( () => {new IntValue_u64(-1n)}, RangeError )
assert.throws( () => {new IntValue_u64(2n**64n + 1n) }, RangeError )
assert.throws( () => {new IntValue_i64(-(2n**63n) - 1n)}, RangeError )
assert.throws( () => {new IntValue_i64(2n**63n)}, RangeError )
assert.throws(() => { new IntValue_u64(-1n) }, RangeError)
assert.throws(() => { new IntValue_u64(2n ** 64n + 1n) }, RangeError)
assert.throws(() => { new IntValue_i64(-(2n ** 63n) - 1n) }, RangeError)
assert.throws(() => { new IntValue_i64(2n ** 63n) }, RangeError)

const x_u64 = new IntValue_u64(2n**40n)
const x_u64 = new IntValue_u64(2n ** 40n)
assert(x_u64.isValid)
assert(x_u64.type === "u64")
assert(x_u64.val === 2n**40n)
assert(x_u64.val === 2n ** 40n)

const x_i64 = new IntValue_i64(-(2n**40n))
const x_i64 = new IntValue_i64(-(2n ** 40n))
assert(x_i64.isValid)
assert(x_i64.type === "i64")
assert(x_i64.val === -(2n**40n))
assert(x_i64.val === -(2n ** 40n))
})

it('asserts 128-bit integer constructors respect bounds', () => {
assert.throws( () => {new IntValue_u128(-1n)}, RangeError )
assert.throws( () => {new IntValue_u128(2n**128n + 1n)}, RangeError )
assert.throws( () => {new IntValue_i128(-(2n**127n) - 1n)}, RangeError )
assert.throws( () => {new IntValue_i128(2n**127n)}, RangeError )
assert.throws(() => { new IntValue_u128(-1n) }, RangeError)
assert.throws(() => { new IntValue_u128(2n ** 128n + 1n) }, RangeError)
assert.throws(() => { new IntValue_i128(-(2n ** 127n) - 1n) }, RangeError)
assert.throws(() => { new IntValue_i128(2n ** 127n) }, RangeError)

const x_u128 = new IntValue_u128(2n**80n)
const x_u128 = new IntValue_u128(2n ** 80n)
assert(x_u128.isValid)
assert(x_u128.type === "u128")
assert(x_u128.val === 2n**80n)
assert(x_u128.val === 2n ** 80n)

const x_i128 = new IntValue_i128(-(2n**80n))
const x_i128 = new IntValue_i128(-(2n ** 80n))
assert(x_i128.isValid)
assert(x_i128.type === "i128")
assert(x_i128.val === -(2n**80n))
assert(x_i128.val === -(2n ** 80n))
})
})

describe('Stringlike tests', () => {
it('asserts SymValue constructors properly check requirements', () => {
assert.throws( () => {new SymValue("waaaaaaaaaaaaaaaaay tooooooooooooooooooooooooo loooooooooooooooooooooooooooooong")}, TypeError )

assert.throws( () => {new SymValue('\u2615')}, TypeError )
assert.throws(() => { new SymValue("waaaaaaaaaaaaaaaaay tooooooooooooooooooooooooo loooooooooooooooooooooooooooooong") }, TypeError)
assert.throws(() => { new SymValue('\u2615') }, TypeError)

const s = new SymValue("FOO")
assert(s.val === 'FOO')
})

it('asserts Address constructors properly check requirements', () => {
assert.throws( () => {new AddrValue("LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG")}, TypeError )

assert.throws( () => {new AddrValue('========================================================')}, TypeError )

assert.throws( () => {new AddrValue('FOO')}, TypeError )
assert.throws(() => { new AddrValue("LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG") }, TypeError)
assert.throws(() => { new AddrValue('========================================================') }, TypeError)
assert.throws(() => { new AddrValue('FOO') }, TypeError)

const s = new AddrValue("ALICE000000000000000000000000000000000000000000000000000")
assert(s.val === "ALICE000000000000000000000000000000000000000000000000000")
Expand Down

0 comments on commit fe24c19

Please sign in to comment.