on master help sign in
Got basic jam an cue working
ace1fc2iko
dev/test.sh+1−1
@@ -4,4 +4,4 @@ set -e
4
4
5cd "$(dirname "$0")/.."
5cd "$(dirname "$0")/.."
6
6
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 @@
15 "elm-community/list-extra": "8.7.0 <= v < 9.0.0"
15 "elm-community/list-extra": "8.7.0 <= v < 9.0.0"
16 },
16 },
17 "test-dependencies": {
17 "test-dependencies": {
18− "elm-explorations/test": "1.0.0 <= v < 2.0.0"
18+ "elm-explorations/test": "2.1.1 <= v < 3.0.0"
19 }
19 }
20}
20}
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)
2
2
3import BitParser as BP exposing (BitParser)
3import BitParser as BP exposing (BitParser)
4import BitWriter as BW exposing (BitWriter)
4import BitWriter as BW exposing (BitWriter)
@@ -13,6 +13,52 @@ type Noun
13 | Atom Bytes
13 | Atom Bytes
14
14
15
15
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+
16isSig : Bytes -> Bool
62isSig : Bytes -> Bool
17isSig bytes =
63isSig bytes =
18 Bytes.toByteValues bytes |> List.all (\x -> x == 0)
64 Bytes.toByteValues bytes |> List.all (\x -> x == 0)
tests/Test/Urbit.elm+35−9
@@ -4,6 +4,7 @@ import BitParser
4import BitWriter
4import BitWriter
5import Bytes exposing (Bytes)
5import Bytes exposing (Bytes)
6import Bytes.Extra as Bytes
6import Bytes.Extra as Bytes
7+import Expect exposing (Expectation)
7import Fuzz exposing (Fuzzer)
8import Fuzz exposing (Fuzzer)
8import List.Extra as List
9import List.Extra as List
9import Test exposing (..)
10import Test exposing (..)
@@ -19,9 +20,37 @@ tests =
19 (\a ->
20 (\a ->
20 maybeBytesEq (Just a) (BitWriter.run (mat a BitWriter.empty) |> BitParser.run rub)
21 maybeBytesEq (Just a) (BitWriter.run (mat a BitWriter.empty) |> BitParser.run rub)
21 )
22 )
23+ , fuzz (noun ())
24+ "jam <-> cue"
25+ (\n ->
26+ maybeNounEq (Just n) (BitWriter.run (jam n BitWriter.empty) |> BitParser.run cue)
27+ )
22 ]
28 ]
23
29
24
30
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+
25atom : Fuzzer Bytes
54atom : Fuzzer Bytes
26atom =
55atom =
27 bytes
56 bytes
@@ -29,12 +58,9 @@ atom =
29 (\a -> a |> Bytes.toByteValues |> List.dropWhileRight (\x -> x == 0) |> Bytes.fromByteValues)
58 (\a -> a |> Bytes.toByteValues |> List.dropWhileRight (\x -> x == 0) |> Bytes.fromByteValues)
30
59
31
60
32−noun : Fuzzer Noun
61+noun : () -> Fuzzer Noun
33−noun =
62+noun () =
34− (\() ->
63+ Fuzz.frequency
35− Fuzz.oneOf
36− [ bytes |> Fuzz.map Atom
64+ [ ( 0.6, atom |> Fuzz.map Atom )
37− , Fuzz.map2 (\a b -> Cell ( a, b )) noun noun
65+ , ( 0.4, Fuzz.map2 (\a b -> Cell ( a, b )) (Fuzz.lazy noun) (Fuzz.lazy noun) )
38− ]
66+ ]
39− )
40− ()