tomo/test/structs.tm

84 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2024-02-24 11:42:11 -08:00
struct Single(x:Int)
2024-02-24 11:42:11 -08:00
struct Pair(x,y:Int)
2024-03-03 15:15:45 -08:00
struct Mixed(x:Int, text:Text)
2025-04-04 15:29:09 -07:00
struct LinkedList(x:Int, next:@LinkedList?=none)
2024-04-12 10:09:31 -07:00
struct Password(text:Text; secret)
2024-02-24 11:42:11 -08:00
2024-05-14 10:30:46 -07:00
struct CorecursiveA(other:@CorecursiveB?)
2025-04-04 15:29:09 -07:00
struct CorecursiveB(other:@CorecursiveA?=none)
2024-05-14 10:30:46 -07:00
2025-04-06 13:07:23 -07:00
func test_literals()
>> Single(123)
= Single(123)
2024-02-24 11:42:11 -08:00
>> x := Pair(10, 20)
= Pair(x=10, y=20)
>> y := Pair(y=20, 10)
= Pair(x=10, y=20)
>> x == y
= yes
>> x == Pair(-1, -2)
= no
2025-04-06 13:07:23 -07:00
func test_metamethods()
2024-02-24 11:42:11 -08:00
>> x := Pair(10, 20)
>> y := Pair(100, 200)
>> x == y
= no
>> x == Pair(10, 20)
= yes
>> x == Pair(10, 30)
= no
>> x < Pair(11, 20)
= yes
2025-04-06 13:34:23 -07:00
>> set := |x|
>> set.has(x)
= yes
>> set.has(y)
= no
2024-02-24 11:42:11 -08:00
2025-04-06 13:07:23 -07:00
func test_mixed()
2024-02-24 11:42:11 -08:00
>> x := Mixed(10, "Hello")
>> y := Mixed(99, "Hello")
>> x == y
= no
2024-02-24 12:29:35 -08:00
>> x == Mixed(10, "Hello")
2024-02-24 11:42:11 -08:00
= yes
2024-02-24 12:29:35 -08:00
>> x == Mixed(10, "Bye")
2024-02-24 11:42:11 -08:00
= no
2024-02-24 12:29:35 -08:00
>> x < Mixed(11, "Hello")
2024-02-24 11:42:11 -08:00
= yes
2025-04-06 13:34:23 -07:00
>> set := |x|
>> set.has(x)
= yes
>> set.has(y)
= no
2024-02-24 11:42:11 -08:00
2025-04-06 13:07:23 -07:00
func test_text()
>> b := @CorecursiveB()
>> a := @CorecursiveA(b)
>> b.other = a
>> a
# = @CorecursiveA(@CorecursiveB(@~1))
2025-04-06 13:07:23 -07:00
func main()
2024-04-12 10:09:31 -07:00
test_literals()
test_metamethods()
test_mixed()
test_text()
2024-03-09 16:06:09 -08:00
2024-04-12 10:09:31 -07:00
>> @LinkedList(10, @LinkedList(20))
>> my_pass := Password("Swordfish")
>> "$my_pass"
= "Password(...)"
>> users_by_password := {my_pass="User1", Password("xxx")="User2"}
>> "$users_by_password"
= '{Password(...)="User1", Password(...)="User2"}'
2024-11-30 12:50:54 -08:00
>> users_by_password[my_pass]!
2024-04-12 10:09:31 -07:00
= "User1"
2024-03-09 16:06:09 -08:00
2024-05-14 10:30:46 -07:00
>> CorecursiveA(@CorecursiveB())