This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add SnapshotTest
- Loading branch information
Remco Bloemen
committed
Aug 24, 2017
1 parent
413543e
commit c0ffee4
Showing
5 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
pragma solidity ^0.4.11; | ||
|
||
import 'snapshottoken/contracts/Snapshot/Snapshot.sol'; | ||
import 'snapshottoken/contracts/Snapshot/DailyAndSnapshotable.sol'; | ||
|
||
contract SnapshotTest is | ||
Snapshot, | ||
DailyAndSnapshotable | ||
{ | ||
Values[] val; | ||
|
||
function curDays() | ||
public | ||
constant | ||
returns (uint) | ||
{ | ||
return block.timestamp / 1 days; | ||
} | ||
|
||
function hasValue() | ||
public | ||
constant | ||
returns (bool) | ||
{ | ||
return hasValue(val); | ||
} | ||
|
||
|
||
function getValue(uint256 def) | ||
public | ||
constant | ||
returns (uint256) | ||
{ | ||
return getValue(val, def); | ||
} | ||
|
||
function hasValueAt(uint256 snapshot) | ||
public | ||
constant | ||
returns (bool) | ||
{ | ||
return hasValueAt(val, snapshot); | ||
} | ||
|
||
function getValueAt(uint256 snapshot, uint256 def) | ||
public | ||
constant | ||
returns (uint256) | ||
{ | ||
return getValueAt(val, snapshot, def); | ||
} | ||
|
||
function setValue(uint256 x) | ||
public | ||
{ | ||
setValue(val, x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import gasCost from "./helpers/gasCost"; | ||
import { latestTimestamp } from "./helpers/latestTime"; | ||
import increaseTime from "./helpers/increaseTime"; | ||
import moment from "moment"; | ||
import expectThrow from "./helpers/expectThrow"; | ||
|
||
const SnapshotTest = artifacts.require("./test/SnapshotTest.sol"); | ||
|
||
const day = 24 * 3600; | ||
|
||
contract("Snapshot", accounts => { | ||
let value; | ||
|
||
const createSnapshot = async () => { | ||
const r = await value.createSnapshot(); | ||
assert.equal(r.logs.length, 1); | ||
assert.equal(r.logs[0].event, "SnapshotCreated"); | ||
return r.logs[0].args.snapshot; | ||
}; | ||
|
||
beforeEach(async () => { | ||
value = await SnapshotTest.new(); | ||
}); | ||
|
||
it("should deploy", async () => { | ||
console.log(`\tSnapshot took ${gasCost(value)}.`); | ||
}); | ||
|
||
it("should be initially unset", async () => { | ||
assert.isFalse(await value.hasValue.call()); | ||
}); | ||
|
||
it("should initially return default", async () => { | ||
assert.isFalse(await value.hasValue.call()); | ||
}); | ||
|
||
it("should initially return default", async () => { | ||
assert.equal(12, await value.getValue.call(12)); | ||
assert.equal(42, await value.getValue.call(42)); | ||
}); | ||
|
||
it("should create a snapshot", async () => { | ||
const r = await value.createSnapshot(); | ||
console.log(`\tcreateSnapshot took ${gasCost(r)}`); | ||
}); | ||
|
||
it("should set a new value", async () => { | ||
const r = await value.setValue(1234); | ||
console.log(`\tsetValue took ${gasCost(r)}`); | ||
|
||
assert.equal(1234, await value.getValue.call(12)); | ||
assert.isTrue(await value.hasValue.call()); | ||
}); | ||
|
||
it("should reset value", async () => { | ||
await value.setValue(1234); | ||
|
||
const r = await value.setValue(12345); | ||
console.log(`\tsetValue took ${gasCost(r)}`); | ||
|
||
assert.equal(12345, await value.getValue.call(12)); | ||
assert.isTrue(await value.hasValue.call()); | ||
}); | ||
|
||
it("should keep values in snapshots", async () => { | ||
const before = await createSnapshot(); | ||
await value.setValue(100); | ||
const middle = await createSnapshot(); | ||
await value.setValue(200); | ||
const after = await createSnapshot(); | ||
|
||
assert.isFalse(await value.hasValueAt.call(before)); | ||
assert.isTrue(await value.hasValueAt.call(middle)); | ||
assert.isTrue(await value.hasValueAt.call(after)); | ||
assert.equal(41, await value.getValueAt.call(before, 41)); | ||
assert.equal(100, await value.getValueAt.call(middle, 41)); | ||
assert.equal(200, await value.getValueAt.call(after, 41)); | ||
}); | ||
|
||
it("should create daily snapshots", async () => { | ||
const day0 = await value.snapshotAt.call(latestTimestamp() + 0 * day); | ||
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day); | ||
const day2 = await value.snapshotAt.call(latestTimestamp() + 2 * day); | ||
const day3 = await value.snapshotAt.call(latestTimestamp() + 3 * day); | ||
await value.setValue(100); | ||
await increaseTime(moment.duration({ days: 1 })); | ||
await value.setValue(200); | ||
await increaseTime(moment.duration({ days: 1 })); | ||
await value.setValue(300); | ||
|
||
assert.equal(41, await value.getValueAt.call(day0, 41)); | ||
assert.equal(100, await value.getValueAt.call(day1, 41)); | ||
assert.equal(200, await value.getValueAt.call(day2, 41)); | ||
}); | ||
|
||
it("should throw when queried in the future", async () => { | ||
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day); | ||
await expectThrow(value.getValueAt.call(day1, 41)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters