on master help sign in
Got basic jam an cue working
commitace1fc22ffdb8127e1f8d8ce5017c54bd8a771ad authoriko <mail@iko.soy> dateSun 28 May 2023, 14:38 (3 years ago) parent636da10
4 files changed+84−12
dev/test.sh+1−1 elm.json+1−1 src/Urbit.elm+47−1 tests/Test/Urbit.elm+35−9
dev/test.sh+1−1
@@ -4,4 +4,4 @@ set -e
44
55cd "$(dirname "$0")/.."
66
7−nix run github:NixOS/nixpkgs#elmPackages.elm-test-rs -- "$@"
7+nix run github:NixOS/nixpkgs#elmPackages.elm-test -- "$@"
elm.json+1−1
@@ -15,6 +15,6 @@
1515 "elm-community/list-extra": "8.7.0 <= v < 9.0.0"
1616 },
1717 "test-dependencies": {
18− "elm-explorations/test": "1.0.0 <= v < 2.0.0"
18+ "elm-explorations/test": "2.1.1 <= v < 3.0.0"
1919 }
2020}
src/Urbit.elm+47−1
@@ -1,4 +1,4 @@
1−module Urbit exposing (Noun(..), mat, rub)
1+module Urbit exposing (Noun(..), cue, jam, mat, rub)
22
33import BitParser as BP exposing (BitParser)
44import BitWriter as BW exposing (BitWriter)
@@ -13,6 +13,52 @@ type Noun
1313 | Atom Bytes
1414
1515
16+jam : Noun -> BitWriter -> BitWriter
17+jam noun writer =
18+ case noun of
19+ Atom atom ->
20+ writer
21+ |> BW.bit 0
22+ |> mat atom
23+
24+ Cell ( a, b ) ->
25+ writer
26+ |> BW.bit 1
27+ |> BW.bit 0
28+ |> jam a
29+ |> jam b
30+
31+
32+cue : BitParser Noun
33+cue =
34+ BP.bit
35+ |> BP.andThen
36+ (\isAtom ->
37+ if isAtom == 0 then
38+ rub |> BP.map Atom
39+
40+ else
41+ BP.bit
42+ |> BP.andThen
43+ (\isRef ->
44+ if isRef == 0 then
45+ cue
46+ |> BP.andThen
47+ (\a ->
48+ cue
49+ |> BP.andThen
50+ (\b ->
51+ Cell ( a, b )
52+ |> BP.succeed
53+ )
54+ )
55+
56+ else
57+ BP.fail
58+ )
59+ )
60+
61+
1662isSig : Bytes -> Bool
1763isSig bytes =
1864 Bytes.toByteValues bytes |> List.all (\x -> x == 0)
tests/Test/Urbit.elm+35−9
@@ -4,6 +4,7 @@ import BitParser
44import BitWriter
55import Bytes exposing (Bytes)
66import Bytes.Extra as Bytes
7+import Expect exposing (Expectation)
78import Fuzz exposing (Fuzzer)
89import List.Extra as List
910import Test exposing (..)
@@ -19,9 +20,37 @@ tests =
1920 (\a ->
2021 maybeBytesEq (Just a) (BitWriter.run (mat a BitWriter.empty) |> BitParser.run rub)
2122 )
23+ , fuzz (noun ())
24+ "jam <-> cue"
25+ (\n ->
26+ maybeNounEq (Just n) (BitWriter.run (jam n BitWriter.empty) |> BitParser.run cue)
27+ )
2228 ]
2329
2430
31+maybeNounEq : Maybe Noun -> Maybe Noun -> Expectation
32+maybeNounEq a b =
33+ case ( a, b ) of
34+ ( Just x, Just y ) ->
35+ nounEq x y
36+
37+ _ ->
38+ Expect.equal a b
39+
40+
41+nounEq : Noun -> Noun -> Expectation
42+nounEq a b =
43+ case ( a, b ) of
44+ ( Atom x, Atom y ) ->
45+ bytesEq x y
46+
47+ ( Cell ( x1, y1 ), Cell ( x2, y2 ) ) ->
48+ Expect.all [ nounEq x1 x2 |> always, nounEq y1 y2 |> always ] ()
49+
50+ _ ->
51+ Expect.fail (Debug.toString a ++ " /= " ++ Debug.toString b)
52+
53+
2554atom : Fuzzer Bytes
2655atom =
2756 bytes
@@ -29,12 +58,9 @@ atom =
2958 (\a -> a |> Bytes.toByteValues |> List.dropWhileRight (\x -> x == 0) |> Bytes.fromByteValues)
3059
3160
32−noun : Fuzzer Noun
33−noun =
34− (\() ->
35− Fuzz.oneOf
36− [ bytes |> Fuzz.map Atom
37− , Fuzz.map2 (\a b -> Cell ( a, b )) noun noun
38− ]
39− )
40− ()
61+noun : () -> Fuzzer Noun
62+noun () =
63+ Fuzz.frequency
64+ [ ( 0.6, atom |> Fuzz.map Atom )
65+ , ( 0.4, Fuzz.map2 (\a b -> Cell ( a, b )) (Fuzz.lazy noun) (Fuzz.lazy noun) )
66+ ]