Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions test/random/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ addEmbedderArgs WasmTime = ("--disable-cache" :) . ("--cranelift" :)
embedder :: Embedder
embedder = WasmTime

withPrim :: Line -> Line
withPrim = (fromString "import Prim \"mo:prim\";" <>)

(runScriptNoFuzz, runScriptWantFuzz) = (runner ExitSuccess id, runner (ExitFailure 1) not)
where runner reqOutcome relevant name testCase =
let as = name <.> "as"
let as = name <.> "mo"
wasm = name <.> "wasm"
fileArg = fromString . encodeString
script = do Turtle.output as $ fromString testCase
script = do Turtle.output as $ withPrim <$> fromString testCase
res@(exitCode, _, _) <- procStrictWithErr "moc"
["-no-system-api", "-no-check-ir", fileArg as] empty
if ExitSuccess == exitCode
Expand All @@ -93,7 +96,7 @@ prop_explodeConcat :: UTF8 String -> Property
prop_explodeConcat (UTF8 str) = monadicIO $ do
let testCase :: String
testCase = "{ var str = \"\"; for (c in \""
<> s <> "\".chars()) { str #= charToText c }; assert (str == \"" <> s <> "\") }"
<> s <> "\".chars()) { str #= Prim.charToText c }; assert (str == \"" <> s <> "\") }"

s = concatMap escape str
runScriptNoFuzz "explodeConcat" testCase
Expand All @@ -115,7 +118,7 @@ escape '"' = "\\\""
escape ch = pure ch

prop_charToText (UTF8 char) = monadicIO $ do
let testCase = "assert (switch ((charToText '"
let testCase = "assert (switch ((Prim.charToText '"
<> c <> "').chars().next()) { case (?'" <> c <> "') true; case _ false })"

c = escape char
Expand Down Expand Up @@ -212,7 +215,7 @@ instance Arbitrary TestCase where


prop_verifies (TestCase (map fromString -> testCase)) = monadicIO $ do
let script cases = do Turtle.output "tests.mo" $ msum cases
let script cases = do Turtle.output "tests.mo" $ msum (pure (withPrim mempty) : cases)
res@(exitCode, _, _) <- procStrictWithErr "moc"
["-no-system-api", "-no-check-ir", "tests.mo"] empty
if ExitSuccess == exitCode
Expand Down Expand Up @@ -971,7 +974,7 @@ unparseMO f@Five = annot f "5"
unparseMO a@(Neuralgic n) = annot a $ literal n
unparseMO (Pos n) = "(+" <> unparseMO n <> ")"
unparseMO (Neg n) = "(-" <> unparseMO n <> ")"
unparseMO (Abs n) = "(abs " <> unparseMO n <> ")"
unparseMO (Abs n) = "(Prim.abs " <> unparseMO n <> ")"
unparseMO (a `Add` b) = inParens unparseMO "+" a b
unparseMO (a `Sub` b) = annot a $ inParens unparseMO "-" a b
unparseMO (a `Mul` b) = inParens unparseMO "*" a b
Expand All @@ -986,16 +989,16 @@ unparseMO (a `RotR` b) = inParens unparseMO "<>>" a b
unparseMO (a `ShiftL` b) = inParens unparseMO "<<" a b
unparseMO (a `ShiftR` b) = inParens unparseMO ">>" a b
unparseMO (a `ShiftRSigned` b) = inParens unparseMO "+>>" a b
unparseMO (PopCnt n) = sizeSuffix n "(popcntWord" <> " " <> unparseMO n <> ")"
unparseMO (Clz n) = sizeSuffix n "(clzWord" <> " " <> unparseMO n <> ")"
unparseMO (Ctz n) = sizeSuffix n "(ctzWord" <> " " <> unparseMO n <> ")"
unparseMO (PopCnt n) = sizeSuffix n "(Prim.popcntWord" <> " " <> unparseMO n <> ")"
unparseMO (Clz n) = sizeSuffix n "(Prim.clzWord" <> " " <> unparseMO n <> ")"
unparseMO (Ctz n) = sizeSuffix n "(Prim.ctzWord" <> " " <> unparseMO n <> ")"
unparseMO (Complement a) = "(^ " <> unparseMO a <> ")"
unparseMO (ConvertNatural a) = "(++++(" <> unparseMO a <> "))"
unparseMO (ConvertNat a) = unparseNat Proxy a
unparseMO (ConvertInt a) = unparseInt Proxy a
unparseMO (ConvertWord a) = unparseWord Proxy a
unparseMO (ConvertWordToNat a) = sizeSuffix a "(word" <> "ToNat " <> unparseMO a <> ")"
unparseMO t@(ConvertNatToWord a) = sizeSuffix t "(natToWord" <> " " <> unparseMO a <> ")"
unparseMO (ConvertWordToNat a) = sizeSuffix a "(Prim.word" <> "ToNat " <> unparseMO a <> ")"
unparseMO t@(ConvertNatToWord a) = sizeSuffix t "(Prim.natToWord" <> " " <> unparseMO a <> ")"
unparseMO (IfThenElse a b c) = "(if (" <> unparseMO c <> ") " <> unparseMO a <> " else " <> unparseMO b <> ")"
unparseMO (a `NotEqual` b) = inParens unparseMO "!=" a b
unparseMO (a `Equals` b) = inParens unparseMO "==" a b
Expand All @@ -1016,13 +1019,13 @@ unparseMO (Text a) = '"' : (concatMap escape a) <> "\""
unparseMO (a `Concat` b) = "(" <> unparseMO a <> " # " <> unparseMO b <> ")"

unparseNat :: KnownNat n => Proxy n -> MOTerm (BitLimited n Natural) -> String
unparseNat p a = "(nat" <> bitWidth p <> "ToNat(" <> unparseMO a <> "))"
unparseNat p a = "(Prim.nat" <> bitWidth p <> "ToNat(" <> unparseMO a <> "))"

unparseInt :: KnownNat n => Proxy n -> MOTerm (BitLimited n Integer) -> String
unparseInt p a = "(int" <> bitWidth p <> "ToInt(" <> unparseMO a <> "))"
unparseInt p a = "(Prim.int" <> bitWidth p <> "ToInt(" <> unparseMO a <> "))"

unparseWord :: KnownNat n => Proxy n -> MOTerm (BitLimited n Word) -> String
unparseWord p a = "(word" <> bitWidth p <> "ToNat(" <> unparseMO a <> "))" -- TODO we want signed too: wordToInt
unparseWord p a = "(Prim.word" <> bitWidth p <> "ToNat(" <> unparseMO a <> "))" -- TODO we want signed too: wordToInt

-- TODOs:
-- - wordToInt
Expand Down
25 changes: 13 additions & 12 deletions test/repl/ok/outrange-int-nat.stdout.ok
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
Motoko 0.1 interpreter
> let Prim : module {type ErrorCode = {#error; #system}; Array_init : <T>(Nat, T) -> [var T]; Array_tabulate : <T>(Nat, Nat -> T) -> [T]; abs : Int -> Nat; btstWord16 : (Word16, Word16) -> Bool; btstWord32 : (Word32, Word32) -> Bool; btstWord64 : (Word64, Word64) -> Bool; btstWord8 : (Word8, Word8) -> Bool; charToText : Char -> Text; charToWord32 : Char -> Word32; clzWord16 : Word16 -> Word16; clzWord32 : Word32 -> Word32; clzWord64 : Word64 -> Word64; clzWord8 : Word8 -> Word8; ctzWord16 : Word16 -> Word16; ctzWord32 : Word32 -> Word32; ctzWord64 : Word64 -> Word64; ctzWord8 : Word8 -> Word8; debugPrint : Text -> (); debugPrintChar : Char -> (); debugPrintInt : Int -> (); debugPrintNat : Nat -> (); error : Text -> Error; errorCode : Error -> ErrorCode; errorMessage : Error -> Text; idlHash : Text -> Word32; int16ToInt : Int16 -> Int; int16ToWord16 : Int16 -> Word16; int32ToInt : Int32 -> Int; int32ToWord32 : Int32 -> Word32; int64ToInt : Int64 -> Int; int64ToWord64 : Int64 -> Word64; int8ToInt : Int8 -> Int; int8ToWord8 : Int8 -> Word8; intToInt16 : Int -> Int16; intToInt32 : Int -> Int32; intToInt64 : Int -> Int64; intToInt8 : Int -> Int8; intToWord16 : Int -> Word16; intToWord32 : Int -> Word32; intToWord64 : Int -> Word64; intToWord8 : Int -> Word8; nat16ToNat : Nat16 -> Nat; nat16ToWord16 : Nat16 -> Word16; nat32ToNat : Nat32 -> Nat; nat32ToWord32 : Nat32 -> Word32; nat64ToNat : Nat64 -> Nat; nat64ToWord64 : Nat64 -> Word64; nat8ToNat : Nat8 -> Nat; nat8ToWord8 : Nat8 -> Word8; natToNat16 : Nat -> Nat16; natToNat32 : Nat -> Nat32; natToNat64 : Nat -> Nat64; natToNat8 : Nat -> Nat8; natToWord16 : Nat -> Word16; natToWord32 : Nat -> Word32; natToWord64 : Nat -> Word64; natToWord8 : Nat -> Word8; popcntWord16 : Word16 -> Word16; popcntWord32 : Word32 -> Word32; popcntWord64 : Word64 -> Word64; popcntWord8 : Word8 -> Word8; rts_callback_table_count : () -> Nat; rts_callback_table_size : () -> Nat; rts_heap_size : () -> Nat; rts_total_allocation : () -> Nat; rts_version : () -> Text; word16ToInt : Word16 -> Int; word16ToInt16 : Word16 -> Int16; word16ToNat : Word16 -> Nat; word16ToNat16 : Word16 -> Nat16; word32ToChar : Word32 -> Char; word32ToInt : Word32 -> Int; word32ToInt32 : Word32 -> Int32; word32ToNat : Word32 -> Nat; word32ToNat32 : Word32 -> Nat32; word64ToInt : Word64 -> Int; word64ToInt64 : Word64 -> Int64; word64ToNat : Word64 -> Nat; word64ToNat64 : Word64 -> Nat64; word8ToInt : Word8 -> Int; word8ToInt8 : Word8 -> Int8; word8ToNat : Word8 -> Nat; word8ToNat8 : Word8 -> Nat8} = {Array_init = func; Array_tabulate = func; abs = func; btstWord16 = func; btstWord32 = func; btstWord64 = func; btstWord8 = func; charToText = func; charToWord32 = func; clzWord16 = func; clzWord32 = func; clzWord64 = func; clzWord8 = func; ctzWord16 = func; ctzWord32 = func; ctzWord64 = func; ctzWord8 = func; debugPrint = func; debugPrintChar = func; debugPrintInt = func; debugPrintNat = func; error = func; errorCode = func; errorMessage = func; idlHash = func; int16ToInt = func; int16ToWord16 = func; int32ToInt = func; int32ToWord32 = func; int64ToInt = func; int64ToWord64 = func; int8ToInt = func; int8ToWord8 = func; intToInt16 = func; intToInt32 = func; intToInt64 = func; intToInt8 = func; intToWord16 = func; intToWord32 = func; intToWord64 = func; intToWord8 = func; nat16ToNat = func; nat16ToWord16 = func; nat32ToNat = func; nat32ToWord32 = func; nat64ToNat = func; nat64ToWord64 = func; nat8ToNat = func; nat8ToWord8 = func; natToNat16 = func; natToNat32 = func; natToNat64 = func; natToNat8 = func; natToWord16 = func; natToWord32 = func; natToWord64 = func; natToWord8 = func; popcntWord16 = func; popcntWord32 = func; popcntWord64 = func; popcntWord8 = func; rts_callback_table_count = func; rts_callback_table_size = func; rts_heap_size = func; rts_total_allocation = func; rts_version = func; word16ToInt = func; word16ToInt16 = func; word16ToNat = func; word16ToNat16 = func; word32ToChar = func; word32ToInt = func; word32ToInt32 = func; word32ToNat = func; word32ToNat32 = func; word64ToInt = func; word64ToInt64 = func; word64ToNat = func; word64ToNat64 = func; word8ToInt = func; word8ToInt8 = func; word8ToNat = func; word8ToNat8 = func}
> +127 : Int8
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> -128 : Int8
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> +32_767 : Int16
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> -32_768 : Int16
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> +2_147_483_647 : Int32
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> -2_147_483_648 : Int32
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> +9_223_372_036_854_775_807 : Int64
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> -9_223_372_036_854_775_808 : Int64
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> 255 : Nat8
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> 65_535 : Nat16
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> 4_294_967_295 : Nat32
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> 18_446_744_073_709_551_615 : Nat64
> prelude:___: execution error, numeric overflow
> prim:___: execution error, numeric overflow
> -127 : Int8
> -127 : Int8
> -32_767 : Int16
Expand Down
2 changes: 1 addition & 1 deletion test/repl/ok/stateful.stderr.ok
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
stdin:2.9-2.17: syntax error, unclosed text literal
stdin:1.9-1.17: type error, operator not defined for operand types
stdin:2.9-2.17: type error, operator not defined for operand types
Nat
and
Bool
49 changes: 25 additions & 24 deletions test/repl/outrange-int-nat.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
#!/usr/bin/env bash
# Tests that the repl Int* and Nat* types properly trap
${MOC:-$(dirname "$BASH_SOURCE")/../../src/moc} -i <<__END__
intToInt8 0x7F;
intToInt8 0x80;
intToInt8 (-0x80);
intToInt8 (-0x81);
import Prim "mo:prim";
Prim.intToInt8 0x7F;
Prim.intToInt8 0x80;
Prim.intToInt8 (-0x80);
Prim.intToInt8 (-0x81);

intToInt16 0x7FFF;
intToInt16 0x8000;
intToInt16 (-0x8000);
intToInt16 (-0x8001);
Prim.intToInt16 0x7FFF;
Prim.intToInt16 0x8000;
Prim.intToInt16 (-0x8000);
Prim.intToInt16 (-0x8001);

intToInt32 0x7FFFFFFF;
intToInt32 0x80000000;
intToInt32 (-0x80000000);
intToInt32 (-0x80000001);
Prim.intToInt32 0x7FFFFFFF;
Prim.intToInt32 0x80000000;
Prim.intToInt32 (-0x80000000);
Prim.intToInt32 (-0x80000001);

intToInt64 0x7FFFFFFFFFFFFFFF;
intToInt64 0x8000000000000000;
intToInt64 (-0x8000000000000000);
intToInt64 (-0x8000000000000001);
Prim.intToInt64 0x7FFFFFFFFFFFFFFF;
Prim.intToInt64 0x8000000000000000;
Prim.intToInt64 (-0x8000000000000000);
Prim.intToInt64 (-0x8000000000000001);


natToNat8 0xFF;
natToNat8 0x100;
Prim.natToNat8 0xFF;
Prim.natToNat8 0x100;

natToNat16 0xFFFF;
natToNat16 0x10000;
Prim.natToNat16 0xFFFF;
Prim.natToNat16 0x10000;

natToNat32 0xFFFFFFFF;
natToNat32 0x100000000;
Prim.natToNat32 0xFFFFFFFF;
Prim.natToNat32 0x100000000;

natToNat64 0xFFFFFFFFFFFFFFFF;
natToNat64 0x10000000000000000;
Prim.natToNat64 0xFFFFFFFFFFFFFFFF;
Prim.natToNat64 0x10000000000000000;


-127 : Int8;
Expand Down