git.iko.soy/rowtype-yoga/purescript-protobuf
on v4.1.0 help sign in
139 lines 4.8 kB history raw
1module Test.Main where
3import Prelude
5import Control.Apply (lift2)
6import Data.Array (catMaybes, mapMaybe, zipWith)
7import Data.ArrayBuffer.Builder (DataBuff(..), execPut)
8import Data.ArrayBuffer.DataView (part, whole)
9import Data.ArrayBuffer.DataView as DV
10import Data.ArrayBuffer.Typed as TA
11import Data.ArrayBuffer.Typed as Typed
12import Data.ArrayBuffer.Types (Float32Array)
13import Data.Either (Either(..))
14import Data.Float32 (fromNumber, toNumber)
15import Data.Float32 as Float32
16import Data.Foldable (sum)
17import Data.Int64 (Int64)
18import Data.Int64 as Int64
19import Data.Maybe (Maybe(..))
20import Data.Number (abs)
21import Data.Tuple (Tuple(..))
22import Data.UInt as UInt
23import Data.UInt64 (UInt64)
24import Data.UInt64 as UInt64
25import Data.Unfoldable (replicate)
26import Effect (Effect)
27import Protobuf.Internal.Decode as Decode
28import Protobuf.Library (Bytes(..))
29import Test.Assert (assert')
30import Parsing (runParserT)
31import Web.Encoding.TextEncoder as TextEncoder
32import Pack.Msg1 as Pack1
33import Pack.Msg2 as Pack2
34import Pack3.Msg3 as Pack3
35import Pack4.Msg4 as Pack4
36import Pack5.Msg5 as Pack5
38billion' :: Int
39billion' = -1000000000
40billion :: UInt.UInt
41billion = UInt.fromInt 1000000000
42billion2' :: Int64
43billion2' = Int64.fromLowHighBits billion' billion'
44billion2 :: UInt64
45billion2 = UInt64.fromLowHighBits 1000000000 1000000000
47main :: Effect Unit
48main = do
50 textEncoder <- TextEncoder.new
51 let somebytes = TA.buffer $ TextEncoder.encode "some bytes" textEncoder
53 let _ = Pack1.mkMsg1 { f1: Just 1.0 }
55 let _ = (Pack4.mkMsg2 -- this is just a compile test
56 { f1: Just $ Pack4.mkMsg1_Msg2 { nested: Just "nested" }
57 })
59 let _ = (Pack5.mkMsg2 -- this is just a compile test
60 { sum1: Just $ Pack5.Msg2_Sum1_F1 $ Pack5.mkMsg1
61 { sum1: Just $ Pack5.Msg1_Sum1_F3 billion'
62 }
63 })
65 let msg1 = (Pack1.mkMsg1
66 { f1: Just 1234.5
67 , f2: Float32.fromNumber 345.6
68 , f3: Just billion'
69 , f4: Just billion2'
70 , f5: Just billion
71 , f6: Just billion2
72 , f7: Just billion'
73 , f8: Just billion2'
74 , f9: Just billion
75 , f10: Just billion2
76 , f11: Just billion2'
77 , f12: Just true
78 , f13: Just "๐Ÿ"
79 , f14: Just $ Bytes $ Buff somebytes
80 , f15: Just Pack1.Msg1_E1_Three
81 })
83 ab1 <- execPut $ Pack1.putMsg1 msg1
84 let dv1 = DV.whole ab1
85 parseResult1 <- runParserT dv1 $ Pack1.parseMsg1 $ DV.byteLength dv1
86 case parseResult1 of
87 Left err -> assert' ("msg1 parse " <> show err) false
88 Right msg1' -> assert' "msg1 roundtrip" $ msg1 == msg1'
90 let msg2 = (Pack2.mkMsg2
91 { f1: replicate 3 1234.5
92 , f2: catMaybes [Float32.fromNumber 345.6, Float32.fromNumber 345.6]
93 , f3: replicate 3 billion'
94 , f4: replicate 3 billion2'
95 , f5: replicate 3 billion
96 , f6: replicate 3 billion2
97 , f7: replicate 3 billion'
98 , f8: replicate 3 billion2'
99 , f9: replicate 3 billion
100 , f10: replicate 3 billion2
101 , f11: replicate 3 billion2'
102 , f12: replicate 3 true
103 , f13: replicate 3 "๐Ÿ"
104 , f14: replicate 3 $ Bytes $ Buff somebytes
105 , f15: replicate 3 Pack2.Msg2_E1_Three
106 })
108 ab2 <- execPut $ Pack2.putMsg2 msg2
109 let dv2 = DV.whole ab2
110 parseResult2 <- runParserT dv2 $ Pack2.parseMsg2 $ DV.byteLength dv2
111 case parseResult2 of
112 Left err -> assert' ("msg2 parse " <> show err) false
113 Right msg2' -> assert' "msg2 roundtrip" $ msg2 == msg2'
115 let msg3 = (Pack3.mkMsg3
116 { f1: replicate 3 msg1
117 , f2: replicate 3 msg2
118 , f4: replicate 3 (Pack3.mkMsg3_Msg4 { f1: Just 1 })
119 })
121 ab3 <- execPut $ Pack3.putMsg3 msg3
122 let dv3 = DV.whole ab3
123 parseResult3 <- runParserT dv3 $ Pack3.parseMsg3 $ DV.byteLength dv3
124 case parseResult3 of
125 Left err -> assert' ("msg3 parse " <> show err) false
126 Right msg3' -> assert' "msg3 roundtrip" $ msg3 == msg3'
127 buf <- Typed.fromArray (mapMaybe fromNumber [ 1.0, 0.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 ]) :: Effect Float32Array
128 parseResult4 <- runParserT (whole (Typed.buffer buf)) (lift2 Tuple (Decode.decodeFloatArray 8) (Decode.decodeFloatArray 4))
129 case parseResult4 of
130 Left err -> assert' ("floatArray" <> show err) false
131 Right x -> assert' "floatArray roundtrip" $ x == Tuple (mapMaybe fromNumber [1.0,0.0]) (mapMaybe fromNumber [3.0])
132 trimmedcustom <- part (Typed.buffer buf) 3 16
133 parseResult5 <- runParserT trimmedcustom (lift2 Tuple (Decode.decodeFloatArray 8) (Decode.decodeFloatArray 4))
134 let almostEqual a b = sum (zipWith (\x y -> abs (toNumber x - toNumber y)) a b) < 1.0e-5
135 case parseResult5 of
136 Left err -> assert' ("floatArray unaligned " <> show err) false
137 Right (Tuple a b) -> assert' "floatArray unaligned roundtrip" $
138 almostEqual a (mapMaybe fromNumber [8.828180325246348e-44,2.0])
139 && almostEqual b (mapMaybe fromNumber [-8.96831017167883e-44])