spl/test.spl

250 lines
6 KiB
Text

[
"#stream.spl" import
"#http.spl" import
"#messaging.spl" import
"#server.spl" import
"#time.spl" import
"#http/server.spl" import
"SPL tester" =program-name
func main { int | with args ;
def thing
1 anew =thing
"hi" 0 thing:unwrap:set;
def thing2 thing:unwrap List:new:from =thing2
"world" thing2:unwrap:push
"hello" 0 thing2:unwrap:insert
"printing first two words of 'hello hi world' (should be 'hello hi')" println
" " print
0 thing2:unwrap:get print " " print
1 thing2:unwrap:get println
"removing hello" println
thing2:pop-front;
"printing first two words again" println
" " print
0 thing2:unwrap:get print " " print
1 thing2:unwrap:get println
"" println
"testing closures and func-ptrs" println
def thingy
"heya1" =thingy
"thingy println" dyn-read call
"heya2" =thingy
{ |
thingy println
} call
def ptr
&println =ptr
"ptr works" ptr call
&&println =ptr
"ptr-ptr works" ptr call call
thingy:&unwrap =ptr
"unwrap-ptr works" ptr call println
thingy:&&unwrap =ptr
"unwrap-ptr-ptr works" ptr call call println
"" println
"testing if" println
def a "test" =a
def b "test" =b
a b eq dup if {
a " is equal to " b concat concat println
} not if {
a " is not equal to " b concat concat panic
}
a b assert-eq;
"" println
"testing ranges & iterators: (0..30@5) + 1" println
def range 5 (0 30 Range:new):set-step =range
range:iter
{ | 1 + } swap:map
{ | _str println } swap:foreach
"" println
"testing Iter:sum of 5 10s" println
0 5 Range:new:iter
{ | pop 10 } swap:map
:sum
_str println
"" println
"testing MicroMap" println
def map MicroMap:new =map
"hey" "hello" map:set;
"helloworld" "Hello, World" map:set;
"{ " print
{ | with item ;
"'" print
0 item:get print
"': '" print
1 item:get print
"', " print
} map:foreach
"}" println
"" println
"Running with args: " print
argv:iter
{ str | " " concat } swap:map
&print swap:foreach
"" println
"testing stream" println
def file "test.txt" 1 StreamTypes:file:create =file
"hi\n" :to-bytes file:write-exact;
file:close; null =file
"" println
"testing split" println
{ | println } (" " "hello how are you" :split):foreach
"" println
use net:http:Request
catch {
"testing http" println;
def req "data.tudbut.de" 80 "GET" "/spltest" Request:new =req
req:send:body _str println;
} { with e ;
e:message println
"it seems the internet is not available" println
}
"" println
"testing cache" println
2 cached-test _str println
3 cached-test _str println
2 cached-test _str println
3 cached-test _str println
"" println
catch {
"heya" throw
} { with e ;
e:message println
}
"" println
"testing messages" println
def bus messaging:Bus:new =bus
bus:subscribe<"testmsg1" { | with message ; message:name print " called1 1" println }>
bus:subscribe<"testmsg1" { | with message ; message:name print " called1 2" println }>
bus:subscribe<"testmsg2" { | with message ; message:name print " called2 1" println }>
bus:subscribe<"testmsg2" { | with message ; message:name print " called2 2" println }>
"testmsg1" bus:publish
"testmsg2" bus:publish
"testmsg1" bus:publish
"testmsg3" bus:publish
"" println
"testing threads" println
def other-thread-done 0 =other-thread-done
{ |
"i am in the other thread!!!" println
1 =other-thread-done
} fork
while { other-thread-done not } { "waiting for the other thread..." println }
"" println
"testing tcp server" println
" starting server thread" println
{ |
def server "0.0.0.0" 4075 net:server:Types:tcp:create =server
while { 1 } {
def stream server:accept =stream
"Hello!" :to-bytes stream:write-exact;
stream:close;
}
} fork;
50 time:sleep;
" starting client" println;
def client "localhost" 4075 StreamTypes:tcp:create =client
1024 client:read-to-end:to-str println;
" ^ this should say 'Hello!'" println;
"" println
"testing string replace" println;
"aba" "!!" "ababab" :replace println;
" ^ should be !!bab." println;
"aba" "!!" "aababab" :replace println;
" ^ should be a!!bab." println;
"" println;
"testing string split" println;
"ba" "abaabaabaa" :split:iter:join<", "> println;
" ^ should be a, a, a, a" println;
"ba" "abbaabbaababaa" :split:iter:join<", "> println;
" ^ should be ab, ab, a, , a" println;
"" println;
"testing string find" println;
"abba" "ababba" :find println;
"^ should be 2" println;
"" println;
"testing readf" println;
def array
"Hello dear {}, {}?" "Hello dear friend, how are you?" (dup println;) :readf =array
"Person was " 0 array:get concat println;
"Question was " 1 array:get concat println;
"" println;
"testing http server" println;
def server "0.0.0.0" 4076 net:http:Server:new =server
{ |
while { 1 } {
server
:accept
:read
:write-ok
:write-str-body<"Hello! This was written to HTTP!">
:finish
}
} fork
def req "localhost" 4076 "GET" "/spltest" Request:new =req
req:send:body _str println;
5 :foreach<{ | "" println }>
"you now have a chance to connect too: localhost :4075 :4076 - stopping in 5 seconds..." println;
5000 time:sleep;
] dup :len 0 eq not if {
"" println
"!! something went wrong somewhere. the stack is not empty." println
dyn-__dump
}
100
}
func cached-test { mega | 1 "cached-test" cache<{ mega | with i ;
i 2 *
"calculated " i _str concat println
}>}