-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve support for ByteArrays (#21)
* Improve support for ByteArrays Adds: ByteArray * Ord instance * Eq instance * concatenate * empty * singleton * toListByte * Add quickcheck tests for bytearray
- Loading branch information
1 parent
c1b27db
commit f4323cc
Showing
8 changed files
with
116 additions
and
15 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 |
---|---|---|
|
@@ -21,4 +21,4 @@ jobs: | |
cache: enable | ||
|
||
- name: Run tests | ||
run: juvix eval test/Main.juvix | ||
run: make -C test |
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 |
---|---|---|
@@ -1,20 +1,52 @@ | ||
module Anoma.Data.ByteArray; | ||
|
||
import Stdlib.Prelude open; | ||
import Stdlib.Trait.Ord.Eq open using {fromOrdToEq}; | ||
import Anoma.Data.Byte; | ||
import Anoma.Data.ByteArray.Base as ByteArray open using {ByteArray}; | ||
|
||
builtin bytearray | ||
axiom ByteArray : Type; | ||
module Internal; | ||
|
||
builtin bytearray-from-list-byte | ||
axiom mk : List Byte -> ByteArray; | ||
--- For input n, returns 2^n | ||
pow2 : Nat -> Nat | ||
| zero := 1 | ||
| (suc n) := foldl {_} {Nat} {{_}} \ {acc _ := 2 * acc} 1 (0 to n); | ||
|
||
builtin bytearray-length | ||
axiom length : ByteArray -> Nat; | ||
--- The size of a byte in bits | ||
byteSize : Nat := 8; | ||
|
||
syntax alias AnomaContents := Nat; | ||
end; | ||
|
||
builtin anoma-bytearray-to-anoma-contents | ||
axiom toAnomaContents : ByteArray -> AnomaContents; | ||
syntax operator <> cons; | ||
|
||
builtin anoma-bytearray-from-anoma-contents | ||
axiom fromAnomaContents : Nat -> AnomaContents -> ByteArray; | ||
--- Concatenate two ;ByteArray;s. | ||
<> (ba1 ba2 : ByteArray) : ByteArray := | ||
let | ||
ba1Size := ByteArray.length ba1; | ||
ba2Size := ByteArray.length ba2; | ||
ba1Content := ByteArray.toAnomaContents ba1; | ||
ba2Content := ByteArray.toAnomaContents ba2; | ||
in ByteArray.fromAnomaContents | ||
(ba1Size + ba2Size) | ||
(ba2Content * Internal.pow2 (Internal.byteSize * ba1Size) + ba1Content); | ||
|
||
--- Unpack a ;ByteArray; to a list of ;Byte;s. | ||
toListByte (ba : ByteArray) : List Byte := | ||
let | ||
byteMax := Internal.pow2 Internal.byteSize; | ||
go (contents : Nat) : Nat -> List Byte | ||
| zero := [] | ||
| (suc n) := Byte.fromNat (mod contents byteMax) :: go (div contents byteMax) n; | ||
in go (ByteArray.toAnomaContents ba) (ByteArray.length ba); | ||
|
||
empty : ByteArray := ByteArray.mk []; | ||
|
||
singleton (b : Byte) : ByteArray := ByteArray.mk [b]; | ||
|
||
instance | ||
ByteArrayOrdI : Ord ByteArray := mkOrd (Ord.cmp on toListByte); | ||
|
||
instance | ||
ByteArrayEqI : Eq ByteArray := fromOrdToEq; | ||
|
||
open ByteArray public; |
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,20 @@ | ||
module Anoma.Data.ByteArray.Base; | ||
|
||
import Stdlib.Prelude open; | ||
|
||
builtin bytearray | ||
axiom ByteArray : Type; | ||
|
||
builtin bytearray-from-list-byte | ||
axiom mk : List Byte -> ByteArray; | ||
|
||
builtin bytearray-length | ||
axiom length : ByteArray -> Nat; | ||
|
||
syntax alias AnomaContents := Nat; | ||
|
||
builtin anoma-bytearray-to-anoma-contents | ||
axiom toAnomaContents : ByteArray -> AnomaContents; | ||
|
||
builtin anoma-bytearray-from-anoma-contents | ||
axiom fromAnomaContents : Nat -> AnomaContents -> ByteArray; |
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,27 @@ | ||
module ByteArrayTests; | ||
|
||
import Stdlib.Prelude open; | ||
import Anoma.Data.ByteArray as ByteArray open using {ByteArray; <>}; | ||
|
||
import Test.QuickCheck.Arbitrary as Arbitrary open using {Arbitrary; mkArbitrary; runArb}; | ||
import Test.QuickCheck.Gen open using {mkGen}; | ||
import Test.QuickCheckTest as QC; | ||
|
||
instance | ||
ByteArbitraryI : Arbitrary Byte := runArb >> Byte.fromNat |> mkGen |> mkArbitrary; | ||
|
||
mkToListByteId : QC.Test := | ||
QC.mkTest | ||
"mk composed with toListByte is identity" | ||
\ {bs := (ByteArray.mk bs |> ByteArray.toListByte) == bs}; | ||
|
||
concatToListByte : QC.Test := | ||
QC.mkTest | ||
"concat of two ByteArrays is concat of bytes" | ||
\ {bs1 bs2 := | ||
let | ||
ba1 := ByteArray.mk bs1; | ||
ba2 := ByteArray.mk bs2; | ||
in ByteArray.toListByte (ba1 <> ba2) == (bs1 ++ bs2)}; | ||
|
||
allTests : List QC.Test := [mkToListByteId; concatToListByte]; |
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,5 @@ | ||
all: test | ||
|
||
.PHONY: test | ||
test: | ||
od -An -N2 -t u2 /dev/urandom | xargs | juvix eval Main.juvix |
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