Added int64

This commit is contained in:
iko 2023-06-15 10:04:20 +03:00
parent 5b2e78d7c6
commit 5934adc2de
Signed by untrusted user: iko
GPG Key ID: 82C257048D1026F2
4 changed files with 53 additions and 1 deletions

View File

@ -19,7 +19,8 @@
"elm/json": "1.1.3 <= v < 2.0.0",
"elm/url": "1.0.0 <= v < 2.0.0",
"elm-community/list-extra": "8.7.0 <= v < 9.0.0",
"elm-community/maybe-extra": "5.3.0 <= v < 6.0.0"
"elm-community/maybe-extra": "5.3.0 <= v < 6.0.0",
"folkertdev/elm-int64": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {
"elm-explorations/test": "2.1.1 <= v < 3.0.0"

View File

@ -5,6 +5,7 @@ module Ur.Constructor exposing
, float32
, float64
, int
, int64
, listOf
, sig
, signedInt
@ -14,6 +15,7 @@ module Ur.Constructor exposing
import Bitwise
import Bytes exposing (Bytes, Endianness(..))
import Bytes.Encode as BE
import Int64 exposing (Int64)
import Ur exposing (..)
@ -42,6 +44,11 @@ int i =
)
int64 : Int64 -> Noun
int64 i =
Atom (BE.encode (Int64.encoder LE i))
signedInt : Int -> Noun
signedInt i =
int

View File

@ -8,6 +8,7 @@ module Ur.Deconstructor exposing
, float32
, float64
, int
, int64
, list
, llec
, map
@ -25,6 +26,7 @@ import Bytes exposing (Bytes, Endianness(..))
import Bytes.Decode as BD
import Bytes.Encode as BE
import Bytes.Extra
import Int64 exposing (Int64)
import Ur exposing (..)
@ -109,6 +111,31 @@ int =
)
int64 : Deconstructor (Int64 -> a) a
int64 =
Deconstructor
(\x f ->
case x of
Atom bs ->
let
width =
Bytes.width bs
in
if width > 8 then
Nothing
else
BD.decode (Int64.decoder LE)
(BE.encode
(BE.sequence (BE.bytes bs :: List.repeat (8 - width) (BE.unsignedInt8 0)))
)
|> Maybe.map f
Cell _ ->
Nothing
)
signedInt : Deconstructor (Int -> a) a
signedInt =
int

View File

@ -6,6 +6,7 @@ import Bytes exposing (Bytes)
import Bytes.Extra as Bytes
import Expect exposing (Expectation)
import Fuzz exposing (Fuzzer)
import Int64 exposing (Int64)
import List.Extra as List
import Test exposing (..)
import Test.Utils exposing (..)
@ -77,6 +78,16 @@ tests =
(Bytes.fromByteValues [ 0x31, 0x12 ])
)
)
, test "1.686.761.906.334"
(\() ->
Expect.equal
(Just "1686761906334")
(D.runBytes
D.int64
(Bytes.fromByteValues [ 0x80, 0xC9, 0x13, 0x04, 0x5B, 0x17, 0x31 ])
|> Maybe.map Int64.toUnsignedString
)
)
, test "[4 ~[1 2 3]]"
(\() ->
Expect.equal
@ -204,6 +215,7 @@ tests =
(C.tape x)
)
)
, Test.fuzz int64 "int64" (\x -> Expect.equal (Just x) (D.run D.int64 (C.int64 x)))
]
, Test.describe "Ur.Uw"
[ Test.fuzz atom
@ -254,3 +266,8 @@ noun () =
[ ( 0.6, atom |> Fuzz.map Atom )
, ( 0.4, Fuzz.map2 (\a b -> Cell ( a, b )) (Fuzz.lazy noun) (Fuzz.lazy noun) )
]
int64 : Fuzzer Int64
int64 =
Fuzz.int |> Fuzz.andThen (\a -> Fuzz.int |> Fuzz.map (Int64.fromInt32s a))