2024-09-09 19:58:59 +02:00
|
|
|
[
|
2024-10-14 05:02:28 +02:00
|
|
|
"spl/stream.spl" import
|
|
|
|
"spl/http.spl" import
|
|
|
|
"spl/messaging.spl" import
|
|
|
|
"spl/server.spl" import
|
|
|
|
"spl/time.spl" import
|
|
|
|
"spl/httpserver/base.spl" import
|
|
|
|
"spl/linkedlist.spl" import
|
2024-09-23 15:45:52 +02:00
|
|
|
|
2023-02-25 11:37:07 +01:00
|
|
|
|
2023-04-08 17:29:59 +02:00
|
|
|
"SPL tester" =program-name
|
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
func main { int | with args ;
|
2024-09-09 19:58:59 +02:00
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
def thing
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
1 anew =thing
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
"hi" 0 thing:unwrap:set;
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-03-06 03:45:16 +01:00
|
|
|
def thing2 thing:unwrap List:new:from =thing2
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
"world" thing2:unwrap:push
|
2023-02-19 19:34:25 +01:00
|
|
|
"hello" 0 thing2:unwrap:insert
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 19:34:25 +01:00
|
|
|
"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
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 19:34:25 +01:00
|
|
|
"" println
|
|
|
|
"testing closures and func-ptrs" println
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
def thingy
|
|
|
|
"heya1" =thingy
|
|
|
|
"thingy println" dyn-read call
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 04:57:32 +01:00
|
|
|
"heya2" =thingy
|
|
|
|
{ |
|
|
|
|
thingy println
|
|
|
|
} call
|
2023-02-18 18:43:05 +01:00
|
|
|
|
2023-02-19 19:34:25 +01:00
|
|
|
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
|
2023-04-08 09:51:40 +02:00
|
|
|
:sum
|
2023-02-19 19:34:25 +01:00
|
|
|
_str println
|
|
|
|
|
|
|
|
"" println
|
|
|
|
"testing MicroMap" println
|
|
|
|
|
2023-03-06 03:45:16 +01:00
|
|
|
def map MicroMap:new =map
|
2023-02-19 19:34:25 +01:00
|
|
|
"hey" "hello" map:set;
|
|
|
|
"helloworld" "Hello, World" map:set;
|
|
|
|
"{ " print
|
2023-03-06 03:45:16 +01:00
|
|
|
{ | with item ;
|
|
|
|
"'" print
|
|
|
|
0 item:get print
|
|
|
|
"': '" print
|
|
|
|
1 item:get print
|
|
|
|
"', " print
|
|
|
|
} map:foreach
|
2023-02-19 19:34:25 +01:00
|
|
|
"}" println
|
|
|
|
|
|
|
|
"" println
|
2023-02-20 14:33:30 +01:00
|
|
|
"Running with args: " print
|
|
|
|
argv:iter
|
|
|
|
{ str | " " concat } swap:map
|
|
|
|
&print swap:foreach
|
2023-02-25 11:37:07 +01:00
|
|
|
|
|
|
|
"" println
|
|
|
|
"testing stream" println
|
|
|
|
|
|
|
|
def file "test.txt" 1 StreamTypes:file:create =file
|
2023-04-08 09:51:40 +02:00
|
|
|
"hi\n" :to-bytes file:write-exact;
|
2024-09-09 19:58:59 +02:00
|
|
|
file:close; null =file
|
2023-02-25 11:37:07 +01:00
|
|
|
|
2023-03-06 14:56:49 +01:00
|
|
|
"" println
|
|
|
|
"testing split" println
|
2023-04-08 09:51:40 +02:00
|
|
|
{ | println } (" " "hello how are you" :split):foreach
|
2023-02-20 14:33:30 +01:00
|
|
|
"" println
|
2023-02-19 19:34:25 +01:00
|
|
|
|
2024-09-12 19:18:20 +02:00
|
|
|
use net:http:Request
|
2024-09-12 15:17:42 +02:00
|
|
|
catch {
|
2024-09-09 19:58:59 +02:00
|
|
|
"testing http" println;
|
2024-09-06 19:07:11 +02:00
|
|
|
def req "data.tudbut.de" 80 "GET" "/spltest" Request:new =req
|
2024-09-09 19:58:59 +02:00
|
|
|
req:send:body _str println;
|
2024-08-29 09:36:53 +02:00
|
|
|
} { with e ;
|
2023-03-06 15:11:29 +01:00
|
|
|
e:message println
|
|
|
|
"it seems the internet is not available" println
|
|
|
|
}
|
2023-03-06 03:45:16 +01:00
|
|
|
"" println
|
|
|
|
|
2023-04-08 17:29:59 +02:00
|
|
|
"testing cache" println
|
|
|
|
2 cached-test _str println
|
|
|
|
3 cached-test _str println
|
|
|
|
2 cached-test _str println
|
|
|
|
3 cached-test _str println
|
|
|
|
"" println
|
|
|
|
|
2023-03-06 15:11:29 +01:00
|
|
|
catch {
|
|
|
|
"heya" throw
|
2024-08-29 09:36:53 +02:00
|
|
|
} { with e ;
|
2023-04-08 17:29:59 +02:00
|
|
|
e:message println
|
2023-03-06 15:11:29 +01:00
|
|
|
}
|
2023-04-10 02:26:21 +02:00
|
|
|
"" println
|
2023-03-06 15:11:29 +01:00
|
|
|
|
2023-04-10 02:26:21 +02:00
|
|
|
"testing messages" println
|
|
|
|
def bus messaging:Bus:new =bus
|
2024-09-08 12:46:51 +02:00
|
|
|
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 }>
|
2023-04-10 02:26:21 +02:00
|
|
|
"testmsg1" bus:publish
|
|
|
|
"testmsg2" bus:publish
|
|
|
|
"testmsg1" bus:publish
|
|
|
|
"testmsg3" bus:publish
|
2024-09-08 14:27:46 +02:00
|
|
|
|
|
|
|
"" 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 }
|
2024-09-09 16:31:35 +02:00
|
|
|
|
|
|
|
"" 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;
|
2023-04-10 02:26:21 +02:00
|
|
|
|
2024-09-09 19:58:59 +02:00
|
|
|
"" 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;
|
|
|
|
|
2024-09-12 15:17:42 +02:00
|
|
|
"" 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 } {
|
2024-09-12 19:18:20 +02:00
|
|
|
server
|
|
|
|
:accept
|
|
|
|
:read
|
2024-09-12 15:17:42 +02:00
|
|
|
: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;
|
2024-09-29 20:53:01 +02:00
|
|
|
|
|
|
|
"" println;
|
|
|
|
"testing urlencoding";
|
|
|
|
|
|
|
|
"hello! this is a test of URL encoding!" net:http:urlencode dup println;
|
|
|
|
net:http:urldecode println;
|
2024-10-05 15:16:56 +02:00
|
|
|
|
|
|
|
"" println;
|
|
|
|
"testing linked lists" println;
|
|
|
|
|
|
|
|
def list LinkedList:new =list
|
|
|
|
"=> len of an empty list: " print;
|
|
|
|
list:len println;
|
|
|
|
|
|
|
|
"=> len of a list with one element: " print;
|
|
|
|
list:push;<"Hello!">
|
|
|
|
list:len println;
|
|
|
|
|
|
|
|
"=> list should not have a next yet... " print;
|
|
|
|
list:next null eq dup if { "ok" swap } not if { "BAD" } println
|
|
|
|
|
|
|
|
"=> element zero should be 'Hello!': " print list:get<0> println;
|
|
|
|
|
|
|
|
"=> iter of list should start with that too: " print list:iter dup:next println;
|
|
|
|
"=> then should be null: " print :next dup null eq if { pop "ok" } println;
|
|
|
|
|
|
|
|
"=> list should contain 'Hello!': " print list:iter:join<", "> println;
|
|
|
|
|
|
|
|
"=> with new element after that: " print
|
|
|
|
list:push;<"One!!">
|
|
|
|
list:iter:join<", "> println;
|
|
|
|
|
|
|
|
"=> pushing numbers 2..10: " print
|
|
|
|
2 10 Range:new:iter:foreach;<{ | list:push; }>
|
|
|
|
list:iter:join<", "> println;
|
|
|
|
|
|
|
|
"=> popping 9: " print
|
|
|
|
list:pop;
|
|
|
|
list:iter:join<", "> println;
|
|
|
|
"=> removing 5: " print
|
2024-10-14 05:02:28 +02:00
|
|
|
list:remove;<5>
|
2024-10-05 15:16:56 +02:00
|
|
|
list:iter:join<", "> println;
|
|
|
|
"=> popping front: " print
|
2024-10-14 05:02:28 +02:00
|
|
|
list:pop-front;
|
2024-10-05 15:16:56 +02:00
|
|
|
list:iter:join<", "> println;
|
|
|
|
|
|
|
|
"=> inserting 0 back: " print
|
2024-10-14 05:02:28 +02:00
|
|
|
0 list:insert;<0>
|
2024-10-05 15:16:56 +02:00
|
|
|
list:iter:join<", "> println;
|
|
|
|
|
|
|
|
"=> inserting 5 back: " print
|
2024-10-14 05:02:28 +02:00
|
|
|
5 list:insert;<5>
|
2024-10-05 15:16:56 +02:00
|
|
|
list:iter:join<", "> println;
|
|
|
|
|
2024-11-15 11:54:54 +01:00
|
|
|
|
|
|
|
^ println
|
|
|
|
"testing match" println
|
|
|
|
|
|
|
|
"a -> b (0): " print "a" "b" match _str println
|
|
|
|
"a -> a (1): " print "a" "a" match _str println
|
|
|
|
"[a b] -> [a a] (0): " print [ "a" "b" ] [ "a" "a" ] match _str println
|
|
|
|
"[a b] -> [a b] (1): " print [ "a" "b" ] [ "a" "b" ] match _str println
|
|
|
|
|
|
|
|
def mtesta, mtestb
|
|
|
|
|
|
|
|
"a => =mtesta (1): " print "a" &=mtesta match _str println
|
|
|
|
"[a b] => [a =mtesta] (1): " print [ "a" "b" ] [ "a" &=mtesta ] match _str println
|
|
|
|
"[b a] => [a =mtesta] (0): " print [ "b" "a" ] [ "a" &=mtesta ] match _str println
|
|
|
|
"[a b] => [=mtesta a] (0): " print [ "b" "a" ] [ "a" &=mtesta ] match _str println
|
|
|
|
"-> mtesta = (b) " mtesta _str concat println
|
|
|
|
|
2024-11-15 14:34:23 +01:00
|
|
|
"^ok => ^ok (1): " print ^ok => ^ok _str println
|
|
|
|
"^bad => ^ok (0): " print ^bad => ^ok _str println
|
|
|
|
"[^bad a] => [^ok =mtestb] (0): " print [ ^bad "a" ] => [ ^ok &=mtestb ] _str println
|
|
|
|
"[^ok b] => [^ok =mtestb] (1): " print [ ^ok "b" ] => [ ^ok &=mtestb ] _str println
|
2024-11-15 11:54:54 +01:00
|
|
|
|
|
|
|
"-> mtestb = (b) " mtestb _str concat println
|
|
|
|
|
|
|
|
def result, val
|
|
|
|
|
|
|
|
[ ^ok "hello, world" ] =result
|
|
|
|
|
2024-11-15 14:34:23 +01:00
|
|
|
result =>! [ ^ok &=val ]
|
2024-11-15 11:54:54 +01:00
|
|
|
val println
|
|
|
|
|
|
|
|
[ ^error "bad" ] =result
|
|
|
|
|
|
|
|
catch Custom {
|
2024-11-15 14:34:23 +01:00
|
|
|
result =>! [ ^ok &println ]
|
2024-11-15 11:54:54 +01:00
|
|
|
} { with e ;
|
|
|
|
e:message "match unsuccessful" eq if {
|
|
|
|
"err value: " print
|
2024-11-15 14:34:23 +01:00
|
|
|
result =>! [ ^error &println ]
|
2024-11-15 11:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 15:01:20 +01:00
|
|
|
^ok =>? ^error not if { println }
|
|
|
|
|
2024-11-15 11:54:54 +01:00
|
|
|
|
|
|
|
|
2024-09-12 15:17:42 +02:00
|
|
|
|
2024-10-14 05:02:28 +02:00
|
|
|
5 :foreach<{ | pop "" println }>
|
2024-09-12 19:18:20 +02:00
|
|
|
|
2024-09-09 19:58:59 +02:00
|
|
|
|
|
|
|
] dup :len 0 eq not if {
|
|
|
|
"" println
|
|
|
|
"!! something went wrong somewhere. the stack is not empty." println
|
|
|
|
dyn-__dump
|
|
|
|
}
|
2024-10-14 05:02:28 +02:00
|
|
|
"you now have a chance to connect too: localhost :4075 :4076 - stopping in 5 seconds..." println;
|
|
|
|
5000 time:sleep;
|
2023-02-19 04:57:32 +01:00
|
|
|
100
|
2023-02-19 02:03:06 +01:00
|
|
|
}
|
2023-04-08 17:29:59 +02:00
|
|
|
|
2024-09-08 12:49:10 +02:00
|
|
|
func cached-test { mega | 1 "cached-test" cache<{ mega | with i ;
|
2023-04-08 17:29:59 +02:00
|
|
|
i 2 *
|
|
|
|
"calculated " i _str concat println
|
2024-09-08 12:46:51 +02:00
|
|
|
}>}
|