-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: increase coverage (100% functions)
- Loading branch information
1 parent
b1af00d
commit a05c759
Showing
5 changed files
with
254 additions
and
63 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,74 @@ | ||
// Copyright 2023 Cartesi Pte. Ltd. | ||
|
||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import "src/Clock.sol"; | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
contract ClockTest is Test { | ||
using Clock for Clock.State; | ||
using Time for Time.Instant; | ||
|
||
Clock.State clock1; | ||
Clock.State clock2; | ||
|
||
uint64 constant clock1Allowance = 20; | ||
uint64 constant clock2Allowance = 30; | ||
|
||
function setUp() public { | ||
Clock.setNewPaused( | ||
clock1, Time.currentTime(), Time.Duration.wrap(clock1Allowance) | ||
); | ||
Clock.setNewPaused( | ||
clock2, Time.currentTime(), Time.Duration.wrap(clock2Allowance) | ||
); | ||
} | ||
|
||
function testMax() public { | ||
Time.Duration max = clock1.max(clock2); | ||
assertEq( | ||
Time.Duration.unwrap(max), | ||
30, | ||
"should return max of two paused clocks" | ||
); | ||
} | ||
|
||
function testAdvanceClock() public { | ||
assertTrue(clock1.startInstant.isZero(), "clock1 should be set paused"); | ||
|
||
clock1.advanceClock(); | ||
assertTrue( | ||
!clock1.startInstant.isZero(), "clock1 should be set running" | ||
); | ||
|
||
clock1.advanceClock(); | ||
assertTrue(clock1.startInstant.isZero(), "clock1 should be set paused"); | ||
} | ||
|
||
function testTimeLeft() public { | ||
assertTrue(clock1.hasTimeLeft(), "clock1 should have time left"); | ||
|
||
clock1.advanceClock(); | ||
assertTrue(clock1.hasTimeLeft(), "clock1 should have time left"); | ||
|
||
vm.warp(block.timestamp + clock1Allowance - 1); | ||
assertTrue(clock1.hasTimeLeft(), "clock1 should have time left"); | ||
|
||
vm.warp(block.timestamp + clock1Allowance); | ||
assertTrue(!clock1.hasTimeLeft(), "clock1 should run out of time"); | ||
|
||
vm.expectRevert("can't advance clock with no time left"); | ||
clock1.advanceClock(); | ||
} | ||
} |
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
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
Oops, something went wrong.