all repos — hex @ 153649ed7756d94780deec2282e5ec74414cdc6b

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
#!/usr/bin/env hex
 
0x0 "test-count" :
0x0 "successes" :
0x0 "failures" :
() "errors" :
() "fails" :

(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") ==)
    ((0x1 0x2 0x3) 0x1 0x2 slice (0x2 0x3) ==)
    ("hello" 0x0 0x3 slice "hell" ==)
    ;36

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

    ("abdef" "c" 0x2 insert "abcdef" ==)
    ("bcdef" "a" 0x0 insert "abcdef" ==)
    ("abcde" "f" 0x5 insert "abcdef" ==)
    ((0x1 0x2 0x3) 0x4 0x3 insert (0x1 0x2 0x3 0x4) ==)
    ;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 "Symbol '!' requires a string" ==)
    (("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") ==)
    
) "tests" :

; Cleanup
clear


; --- 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 exit
    )
when
(fails len 0x0 >)
    (
        "Failed Tests: " fails ", " join cat warn
        0x1 exit
    )
when


0x0 exit