Skip to content

Commit

Permalink
Extract loop test into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Dec 15, 2023
1 parent 32c8437 commit a4ba207
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 63 deletions.
67 changes: 67 additions & 0 deletions test/loop_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule LoopTest do
use ExUnit.Case, async: true

import Orb, only: [to_wat: 1]

test "loop" do
defmodule FileNameSafe do
use Orb

Memory.pages(2)

wasm do
func get_is_valid(), I32, str: I32.U8.UnsafePointer, char: I32 do
str = 1024

loop Loop, result: I32 do
Control.block Outer do
Control.block :inner do
char = str[at!: 0]
Control.break(:inner, if: I32.eq(char, ?/))
Outer.break(if: char)
return(1)
end

return(0)
end

str = str + 1
Loop.continue()
end
end
end
end

wasm_source = """
(module $FileNameSafe
(memory (export "memory") 2)
(func $get_is_valid (export "get_is_valid") (result i32)
(local $str i32)
(local $char i32)
(i32.const 1024)
(local.set $str)
(loop $Loop (result i32)
(block $Outer
(block $inner
(i32.load8_u (local.get $str))
(local.set $char)
(i32.eq (local.get $char) (i32.const 47))
(br_if $inner)
(local.get $char)
(br_if $Outer)
(return (i32.const 1))
)
(return (i32.const 0))
)
(i32.add (local.get $str) (i32.const 1))
(local.set $str)
(br $Loop)
)
)
)
"""

assert wasm_source == to_wat(FileNameSafe)
# assert Wasm.call(FileNameSafe, "body") == 100
end
end
65 changes: 2 additions & 63 deletions test/orb_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule OrbTest do
use ExUnit.Case, async: true

use Orb
import Orb, only: [to_wat: 1]
alias Orb.{I32, U32, Memory}

test "func" do
import Orb.DSL
Expand Down Expand Up @@ -801,68 +802,6 @@ defmodule OrbTest do
assert {:error, _} = Wasm.call(wat, :declare_defcon, 10)
end

test "loop" do
defmodule FileNameSafe do
use Orb

Memory.pages(2)

wasm do
func get_is_valid(), I32, str: I32.U8.UnsafePointer, char: I32 do
str = 1024

loop Loop, result: I32 do
Control.block Outer do
Control.block :inner do
char = str[at!: 0]
Control.break(:inner, if: I32.eq(char, ?/))
Outer.break(if: char)
return(1)
end

return(0)
end

str = str + 1
Loop.continue()
end
end
end
end

wasm_source = """
(module $FileNameSafe
(memory (export "memory") 2)
(func $get_is_valid (export "get_is_valid") (result i32)
(local $str i32)
(local $char i32)
(i32.const 1024)
(local.set $str)
(loop $Loop (result i32)
(block $Outer
(block $inner
(i32.load8_u (local.get $str))
(local.set $char)
(i32.eq (local.get $char) (i32.const 47))
(br_if $inner)
(local.get $char)
(br_if $Outer)
(return (i32.const 1))
)
(return (i32.const 0))
)
(i32.add (local.get $str) (i32.const 1))
(local.set $str)
(br $Loop)
)
)
)
"""

assert wasm_source == to_wat(FileNameSafe)
# assert Wasm.call(FileNameSafe, "body") == 100
end

defmodule SnippetDefiner do
def hd!(ptr) do
Orb.snippet U32 do
Expand Down

0 comments on commit a4ba207

Please sign in to comment.