add is_power_of_2 implementation

This commit is contained in:
Jacek Sieka 2018-12-05 07:07:42 -06:00
parent 4b41010610
commit 94756cc5a2
No known key found for this signature in database
GPG Key ID: 6299FEB3EB6FA465
6 changed files with 23 additions and 3 deletions

View File

@ -134,4 +134,4 @@ func get_domain*(fork_data: ForkData, slot: uint64, domain_type: uint64): uint64
# TODO Slot overflow? Or is slot 32 bits for all intents and purposes?
(get_fork_version(fork_data, slot) shl 32) + domain_type
func is_power_of_2*(v: uint64): bool = discard # TODO
func is_power_of_2*(v: uint64): bool = (v and (v-1)) == 0

View File

@ -8,5 +8,6 @@
import
./test_beaconstate,
./test_block_processing,
./test_helpers,
./test_ssz,
./test_validator

View File

@ -7,7 +7,7 @@
import
sequtils, unittest,
./testhelpers,
./testutil,
../beacon_chain/extras,
../beacon_chain/spec/[beaconstate, datatypes, digest]

View File

@ -7,7 +7,7 @@
import
options, sequtils, unittest,
./testhelpers,
./testutil,
../beacon_chain/spec/[beaconstate, datatypes, digest],
../beacon_chain/[extras, state_transition]

19
tests/test_helpers.nim Normal file
View File

@ -0,0 +1,19 @@
# beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
sequtils, unittest,
../beacon_chain/spec/[helpers]
suite "Spec helpers":
test "is_power_of_2 should do its job":
check:
is_power_of_2(1) == true
is_power_of_2(2) == true
is_power_of_2(3) == false
is_power_of_2(4) == true
is_power_of_2(not 0'u64) == false