all repos — hex @ e2d0cd28b135657af2a436e643f9b09b114fcd2c

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

scripts/test.hex

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
#!/usr/bin/env hex
 
0x0 "test-count" :
0x0 "successes" :
0x0 "failures" :
() "errors" :
() "fails" :
0x0 "result" :

(dup puts) "_" :

(
    "current-test" :
    test-count 0x1 + "test-count" :
    (
        (current-test .)
            (
                "." print 
                successes 0x1 + "successes" :
            )
            (
                "x" print
                failures 0x1 + "failures" :
                fails test-count dec ' cat "fails" :
            )
        if
    ) 
        (
            ; Store error
            errors " - Test #" test-count dec cat ": " cat error cat ' cat "errors" :  
            "x" print
            failures 0x1 + "failures" :
            fails test-count dec ' cat "fails" :
        )
    try
) "test" :


; --- Define tests
(
    (0x1 "a" : a 0x1 ==)
    ("a" # 0x1)
    ("aaa" type "string" == 0x1 type "integer" == () type "quotation" == and and)
    ((0x1 0x2 +) . 0x3 ==)
    ;4

    ("0x2 0x2 -" ! 0x0 ==)
    (0x4 0x5 + 0x9 ==)
    (0x5 0x3 - 0x2 ==)
    (0x5 0x2 * 0xa ==)
    ;8

    (0x5 0x2 / 0x2 ==)
    (0x4 0x2 % 0x0 ==)
    (0x10101 0x01010 & 0x0 ==)
    (0x10111 0x01000 | 0x11111 ==)
    ;12


    (0x1 0x1 ^ 0x0 ==)
    (0x1 ~ 0xfffffffe ==)
    (0x1 0x4 << 0x10 ==)
    (0x10 0x4 >> 0x1 ==)
    ;16

    ("10" int 0x10 ==)
    (0x10 str "10" ==)
    (0xa dec "10" ==)
    ("10" hex 0xa ==)
    ;20

    ("aaa" "aaa" ==)
    (0x20 0x20 ==)
    (0x21 0x22 !=)
    ("abba" "aaa" !=)
    ;24

    (0x2 0x1 >)
    (0x2 0x2 >=)
    (0x2 0x3 <)
    (0x3 0x3 <=)
    ;28

    (0x2 0x3 and)
    (0x1 0x0 or)
    (0x1 0x0 xor)
    (0x1 0x1 xor not)
    ;32

    ("hello" " world" cat "hello world" ==)
    ((0x1 "a") ("b") cat (0x1 "a" "b") ==)
    ((() not) (error) try "Symbol 'not' requires an integer" ==)
    ((() 0x1 xor) (error) try "Symbol 'xor' requires two integers" ==)
    ;36

    ("aaaaa" len 0x5 ==)
    ((0x1 0x3 puts "aaa") len 0x4 ==)
    ((0x2 0x3 0x4) 0x2 get 0x4 ==)
    ("abcde" 0x1 get "b" ==)
    ;40

    ("a/b/c" "t-path" : (t-path "/" index 0x0 >=) (t-path "/" "\\" replace "t-path" :) while t-path "a\\b\\c" == "t-path" #)
    ("assets\"" "\"" "/" replace "assets/" ==)
    (("" () cat) (error) try "Symbol 'cat' requires two quotations or two strings" ==)
    ((0x4 len) (error) try "Symbol 'len' requires a quotation or a string" ==)
    ;44

    ("this is a test" "is" index 0x2 ==)
    (("a" 0x1 "b") "b" index 0x2 ==)
    ("abc" "d" index 0xffffffff ==)
    (("0" "0" "0" "0") "." join "0.0.0.0"==)
    ;48

    ("0.0.0.0" "." split ("0" "0" "0" "0") ==)
    ("Hello, World!" "Hello" "Goodbye" replace "Goodbye, World!" ==)
    ("test" "test.txt" write "test.txt" read "test" ==)
    ("!" "test.txt" append "test.txt" read "test!" ==)
    ;52

    ("rm test.txt" exec 0x0 ==)
    ("rm test.txt" run 0x0 get 0x1 ==)
    ("ls web/contents" run 0x1 get "\n" split len 0x7 ==)
    (args 0x1 get "scripts/test.hex" ==)
    ;56

    ((args len 0x2 ==) ("two") ("no") if "two" ==)
    ((args len 0x3 !=) ("yes") when "yes" ==)
    (0x1 "tmp-a" : (tmp-a 0x3 <) (tmp-a 0x1 + "tmp-a" :) while tmp-a 0x3 ==)
    (0x0 "tmp-b" : (0x1 0x2 0x3) (tmp-b + "tmp-b" :) each tmp-b 0x6 ==)
    ;60

    ((0x2 0x0 /) (error "Division by zero" ==) try)
    (error "" ==)
    ("a" ' ' ' ((("a"))) ==)
    ((0x1 0x2 0x3) (dup dup * *) map (0x1 0x8 0x1b) ==)
    ;64

    ((0x1 0x2 0x3) (0x2 % 0x0 ==) filter (0x2) ==)
    (0x2 dup stack (0x2 0x2) ==)
    (clear stack () ==)
    (0x1 0x2 swap pop stack (0x2) ==)
    ;68

    (("aaa" "puts" :) (error) try "Failed to store symbol 'puts'" ==)
    (("puts" #) (error) try "Cannot free native symbol 'puts'" ==)
    (("aaa" 0x2 :) (error) try "Symbol name must be a string" ==)
    ((0x2 #) (error) try "Symbol name must be a string" ==)
    ;72

    (("puts" .) (error) try "Symbol '.' requires a quotation" ==)
    (((puts) !) (error) try "Quotation must contain only integers" ==)
    (("3" 0x3 +) (error) try "Symbol '+' requires two integers" ==)
    (("3" 0x3 -) (error) try "Symbol '-' requires two integers" ==)
    ;76

    ((0x2 0x3 0x3) (0x2 0x3) > 0x1 ==)
    ((0x2 0x3) (0x2 0x1) > 0x1 ==)
    ((test abc) (test cde) < 0x1 ==)
    (("test" "abc") ("test" "abc") <= 0x1 ==)
    ;80

    ("hello" "" split ("h" "e" "l" "l" "o") ==)
    (("3" 0x3 /) (error) try "Symbol '/' requires two integers" ==)
    ((0x4 0x0 %) (error) try "Division by zero" ==)
    ((() 0x3 %) (error) try "Symbol '%' requires two integers" ==)
    ;84

    ((() 0x3 &) (error) try "Symbol '&' requires two integers" ==)
    ((0x2 "" |) (error) try "Symbol '|' requires two integers" ==)
    ((() "" ^) (error) try "Symbol '^' requires two integers" ==)
    ((() 0x1 >>) (error) try "Symbol '>>' requires two integers" ==)
    ;88

    ((() 0x1 <<) (error) try "Symbol '<<' requires two integers" ==)
    (("" ~) (error) try "Symbol '~' requires an integer" ==)
    ((0x5 int) (error) try "Symbol 'int' requires a string" ==)
    (((0x3) int) (error) try "Symbol 'int' requires a string" ==)
    ;92

    (("5" str) (error) try "Symbol 'str' requires an integer" ==)
    ((("aaa") str) (error) try "Symbol 'str' requires an integer" ==)
    (("10" dec) (error) try "Symbol 'dec' requires an integer" ==)
    ((0x5 hex) (error) try "Symbol 'hex' requires a string representing a decimal integer" ==)
    ;96

    ("0x2" 0x2 == 0x0 ==)
    ("0x2" (0x2) !=)
    ((0x3 "aaa") ("aaa" 0x5) > 0x0 ==)
    (("aaa" 0x5) ("aaa" 0x5 0x4) >= 0x0 ==)
    ;100

    ((test "aaa") (test "aaa" 0x5) < 0x1 ==) 
    (("aaa" 0x5) ("" "aaa" 0x5) <= 0x0 ==)
    (("a" "b" and) (error) try "Symbol 'and' requires two integers" ==)
    ((0x1 "a" or) (error) try "Symbol 'or' requires two integers" ==)
    ;104

    (("abc" 0x3 get) (error) try "Index out of range" ==)
    (((0x2) 0x2 get) (error) try "Index out of range" ==)
    ((() 0x0 get) (error) try "Index out of range" ==)
    ((0x4 0x4 get) (error) try "Symbol 'get' requires a quotation or a string" ==)
    ;108

    (("abc" "b" get) (error) try "Index must be an integer" ==)
    (((0x4 "aa" test) "b" get) (error) try "Index must be an integer" ==)
    ("a" ord 0x61 ==)
    (0x61 chr "a" ==)
    ;112

    ("=" ord 0x3d ==)
    (0x3d chr "=" ==)
    ("é" ord 0xffffffff ==)
    (0x80 chr "" ==)
    ;116

    ("abc" ord 0xffffffff ==)
    ((0x56 ord) (error) try "Symbol 'ord' requires a string" ==)
    (("t" chr) (error) try "Symbol 'chr' requires an integer" ==)
    ((() chr) (error) try "Symbol 'chr' requires an integer" ==)
    ;120

    (0xffffffff dec "-1" ==)

    
) "tests" :


; --- Run tests
tests (test .) each

; --- Report
"\nSuccessful Tests: " print successes dec print "/" print successes failures + dec puts


(errors len 0x0 >)
    (
        "Errors:" warn
        errors (warn) each
        0x1 "result" :
    )
when
(fails len 0x0 >)
    (
        "Failed Tests: " fails ", " join cat warn
        0x1 "result" :
    )
when


result exit