Skip to content

Commit

Permalink
Fix pow error
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfnjust committed Jan 24, 2024
1 parent dc8cf8b commit be554eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
47 changes: 24 additions & 23 deletions src/contracts/shift.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
import { SmartContractLib, byteString2Int, int2ByteString, method, assert, SmartContract} from "scrypt-ts";
import {
SmartContractLib,
byteString2Int,
int2ByteString,
method,
assert,
SmartContract,
toByteString,
lshift,
} from 'scrypt-ts'

//
export class Shift extends SmartContractLib{

export class Shift extends SmartContractLib {
// return 2^n
@method()
static pow2 (n : bigint) : bigint {

return byteString2Int(
int2ByteString(0n, n/8n)
);
static pow2(n: bigint): bigint {
return lshift(1n, n)
}

// binary left shift number x by n places
@method()
static left (x : bigint, n : bigint) : bigint {

return x * Shift.pow2(n);
static left(x: bigint, n: bigint): bigint {
return x * Shift.pow2(n)
}

// binary right shift number x by n places
@method()
static right (x : bigint, n : bigint) : bigint {

return x / Shift.pow2(n);
static right(x: bigint, n: bigint): bigint {
return x / Shift.pow2(n)
}

}

export class ShiftTest extends SmartContract{

export class ShiftTest extends SmartContract {
@method()
public pow2(n : bigint, x : bigint){
assert(Shift.pow2(n) == x)
public pow2(n: bigint, x: bigint) {
assert(Shift.pow2(n) == x, 'pow2 method failed')
}

@method()
public left(x : bigint, y : bigint, z : bigint){
assert(Shift.left(x,y) == z)
public left(x: bigint, y: bigint, z: bigint) {
assert(Shift.left(x, y) == z, 'left method failed')
}

@method()
public right(x : bigint, y : bigint, z : bigint){
assert(Shift.right(x, y) == z)
public right(x: bigint, y: bigint, z: bigint) {
assert(Shift.right(x, y) == z, 'right method failed')
}
}
20 changes: 7 additions & 13 deletions tests/shift.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FixedArray } from 'scrypt-ts'
import { ShiftTest } from '../src/contracts/shift'
import { getDefaultSigner } from './utils/helper'
import { expect, use } from 'chai'
Expand All @@ -18,58 +17,53 @@ describe('Test SmartContract `ShiftTest`', () => {

const callContract = async () => {
await instance.methods.pow2(3n, 8n)
return expect(callContract()).not.be.rejected
}
return expect(callContract()).not.be.rejected
})

it('left should binary left shift number x by n places', async () => {
await instance.deploy(1)

const callContract = async () => {
await instance.methods.left(2n, 2n, 8n)
return expect(callContract()).not.be.rejected
}
return expect(callContract()).not.be.rejected
})

it('right should binary right shift number x by n places', async () => {
await instance.deploy(1)

const callContract = async () => {
await instance.methods.right(8n, 2n, 2n)
return expect(callContract()).not.be.rejected
}
return expect(callContract()).not.be.rejected
})

it('should throw when calling pow2 method ', async () => {
await instance.deploy(1)

const callContract = async () => {
await instance.methods.pow2(3n, 0n)
return expect(callContract()).to.be.rejectedWith(
/pow2 method failed/
)
}
return expect(callContract()).to.be.rejectedWith(/pow2 method failed/)
})

it('should throw when calling left method ', async () => {
await instance.deploy(1)

const callContract = async () => {
await instance.methods.left(3n, 0n, 0n)
return expect(callContract()).to.be.rejectedWith(
/left method failed/
)
}

return expect(callContract()).to.be.rejectedWith(/left method failed/)
})

it('should throw when calling right method ', async () => {
await instance.deploy(1)

const callContract = async () => {
await instance.methods.right(3n, 0n, 0n)
return expect(callContract()).to.be.rejectedWith(
/right method failed/
)
}
return expect(callContract()).to.be.rejectedWith(/right method failed/)
})
})

0 comments on commit be554eb

Please sign in to comment.