all repos — min @ 3b89675f52cce9a391b61a2467527f5d01982ae0

A small but practical concatenative programming language.

tests/global.min

 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
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
'min-test require :test
;;;

"global" test.describe

  (2 'a define
    (3 a + (5 'a define a) -> +) -> a + 12 ==) test.assert

  (symbols "a" in? false ==) test.assert

  ('abcdefg defined-symbol? false ==) test.assert
  
  5 :five
  (symbols "five" in?) test.assert

  (
    (1 2 3 4 5 6) :test-data

    (
      :item
      "_$1" (item) => % :namesym
      item (dup *) cons namesym define
      namesym global publish
    ) :def

    test-data def foreach

    _2 -> _5 -> _1 -> + +
    30 ==
  ) test.assert
  
  (
    (dup *) ^square
    3 square 9 ==
  ) test.assert
  
  'five delete-symbol
  (symbols "five" in? false  ==) test.assert

  ("3 4 +" eval 7 ==) test.assert

  ("2 2 +" "tests/testload.min" fwrite 'testload load 4 ==) test.assert
  "tests/testload.min" rm

  ("2 :two 3 :three" "tests/testrequire.min" fwrite 'testrequire require :tm tm.two tm.three + 5 ==) test.assert
  "tests/testrequire.min" rm

  (2 quote (2) ==) test.assert

  ((2 3) dequote get-stack (2 3) ==) test.assert

  (3 (succ) 3 times 6 ==) test.assert

  ((2 3 >) ("YES") ("NO") if "NO" ==) test.assert
  ((2 3 <) ("YES") ("NO") if "YES" ==) test.assert

  ("NO" (2 3 >) ("YES") when "NO" ==) test.assert

  ((2 3 <) ("YES") when "YES" ==) test.assert

  ((2 3 >) ("YES") unless "YES" ==) test.assert

  (1 type "int" ==) test.assert
  ({} type "dict" ==) test.assert
  (global type "dict:module" ==) test.assert

  (5 (dup 0 ==) (1 +) (dup 1 -) ( * ) linrec 120 ==) test.assert ;;factorial of 5

  (
   (
    (pop) 
    ('error dget) 
    ("finally")
    ) try get-stack ("MinEmptyStackError" "finally") ==) test.assert 

  ("aaaa" :cd cd "aaaa" ==) test.assert ;;It is possible to shadow sealed symbols in child scopes

  (((2 :a1 'a1 seal-symbol 3 :a1) ("failed")) try "failed" ==) test.assert

  (
    1 :a1
    'a1 seal-symbol
    'a1 unseal-symbol
    2 :a1
    2 a1 ==) test.assert

  (
   (
    ({"TestError" :error "Test Message" :message} raise)
    ("error" dget)
    ) try "TestError" ==) test.assert

  (
   (("test" °test :)) try get-stack ("test") ==) test.assert

  (
   (
    (() 1 get)
    (1)
    ) try 1 ==) test.assert

  ((a b +) {4 :a 5 :b} with 9 ==) test.assert

  ("{\"a\": 1, \"b\": 2.3}" from-json {1 :a 2.3 :b} ==) test.assert

  ((1 2 3 "aaa" 'q q true) to-json "\r\n" "" replace "\n" "" replace " " "" replace "[1,2,3,\"aaa\",\";sym:'q\",\";sym:q\",true]"  ==) test.assert

  ((1 2 3 "aaa" 'q q true) to-json from-json (1 2 3 "aaa" 'q q true)  ==) test.assert

  (((1 2 3)) :sym1 "sym1" save-symbol saved-symbols "sym1" in?) test.assert

  ("sym1" load-symbol symbols "sym1" in?) test.assert

  ('sym1 remove-symbol saved-symbols "sym1" in? false ==) test.assert

  (0 :temp (1 2 3) (temp + @temp) foreach 6 temp ==) test.assert

  (
   (1 +) ^mysucc
   'mysucc source (1 +) ==) test.assert

  (6 
    (
      ((3 ==) (false))
      ((3 <) (false))
      ((3 >) (true))
    ) case
  ) test.assert

  (time scope-symbols ("datetime" "now" "tformat" "timeinfo" "timestamp" "to-timestamp") ==) test.assert

  (global scope-sigils "$" in?) test.assert

  ({3 :a 5 :b} scope-symbols ("a" "b") ==) test.assert

  (dev?)
    ((opts {true :d} ==) test.assert)
    ((opts {} ==) test.assert)
  if

  (args first "\\.min$" match?) test.assert

  (3 string "3" ==) test.assert
  
  ("false" boolean false ==) test.assert
  
  ("" boolean false ==) test.assert
  
  (0 boolean false ==) test.assert
  
  (false boolean false ==) test.assert
  
  (0.0 boolean false ==) test.assert
  
  ("something" boolean true ==) test.assert

  ("345" integer 345 ==) test.assert

  (true integer 1 ==) test.assert

  (3.5 integer 3 ==) test.assert

  (3.5 float 3.5 ==) test.assert

  (3 float 3.0 ==) test.assert

  (false float 0.0 ==) test.assert

  ("3.678" float 3.678 ==) test.assert

  (
    {1 :a 2 :b 3 :c} (
      (dup "a" dget  succ succ "a" dset)
      (dup "b" dget  succ "b" dset)
    ) tap
    {3 :a 3 :b 3 :c} ==
  ) test.assert

  (
    {} :data
    data (
      1 "a" dset
      2 "b" dset
    ) tap
    {1 :a 2 :b} ==
  ) test.assert

  (
    "" :s1
    "test" (
      (' "1" swap append "" join)
      (' "2" swap append "" join)
      (' "3" swap append "" join @s1 s1)
    ) tap!
    s1 "test123" ==
  ) test.assert

  (3.4 "test" 1 (int str num) expect (3.4 "test" 1) ==) test.assert

  ("aaa bbb ccc 2 2 + (2 3 4)" parse (aaa bbb ccc 2 2 + (2 3 4)) ==) test.assert

  ({} :myscope (2 :two 3 :three scope @myscope) -> myscope scope-symbols ("three" "two") ==) test.assert

  ((2 < 3 and (4 > 2)) >< true) test.assert

  ((float 3) >> 3.0 ==) test.assert
  
  (
    (
      sigil del
      (str :in ==> str :out)
      (in "--" suffix "--" prefix @out)
    ) ::
    del"test" "--test--" ==
  ) test.assert
  
  (
    (
      sigil x
      (str :in ==> str :out)
      (in "x" prefix @out)
    ) ::
    'x unseal-sigil 'x delete-sigil 
    'x defined-sigil? false ==
  ) test.assert
  
  (
    {{100 :b} :a} :test test.a.b 100 ==
  ) test.assert
  
  (
    (
      symbol pow-mul
      (num :base int :exp ==> num :pr num :mr)
      ( 
        exp 1 - :n
        base exp * @mr
        base  (dup) n times (*) n times @pr
      )
    ) operator 
    2 4 pow-mul - 8 == 
  ) test.assert
  
  (
    ( 
      sigil nt
      (str :s ==> bool :result)
      (
        ("int" s ==)
          (true @result return)
        when
        ("num" s ==)
          (true @result return)
        when
        ("flt" s ==)
          (true @result return)
        when
        "not gonna be printed if true!" puts
        false @result
      )
    ) ::
    nt"flt"
  ) test.assert

 ;; (:n ((n integer?) (n 0 >)) &&) 'natural typeclass
  (
    typeclass natural
    (int :n ==> bool :out)
    (((n integer?) (n 0 >)) "&" run @out)
  ) ::
  ("typeclass:natural" defined-symbol?) test.assert
  (
    symbol natural-sum
    (natural :n natural :m ==> natural :result)
    (n m + @result)
  ) ::
  (dev?)
    (
      null :err
      (
        (3 -3 natural-sum)
        (@err)
      ) try
      pop ;;Remove 3 that was left on the stack.
      (err format-error "expected: natural natural natural-sum" match?) test.assert
      (2 3 natural-sum 5 ==) test.assert
    )
  when
  
  
  (
   constructor test-c
   (str :in ==> dict :out)
   (
    {}
      in "test" dset
    @out
   )
  ) ::
  ("aaa" test-c 'dict:test-c type?) test.assert
  
  (
    symbol add
    ((str|num|quot :t) :a t :b ==> t :result)
    (
     (a type "str" ==)
       (a b suffix @result return)
     when
     (a type "num" ==)
       (a b + @result return)
     when
     (a type "quot" ==)
       (a b concat @result return)
     when
    )
  ) ::
  ("a" "b" add "ab" ==) test.assert
  ((1 2 3) (4 5) add (1 2 3 4 5) ==) test.assert
  
  
  (typeclass fiveplus
      (int :n ==> bool :o)
      (
        n 5 > @o
      )
  ) ::

  (typeclass tenminus
    (int :n ==> bool :o)
    (
      n 10 < @o
    )
  ) ::

  (typeclass even
    (int :n ==> bool :o)
    (
      n 2 mod 0 == @o
    )
  ) ::
  (
    'tenminus&fiveplus 'five-to-ten typealias
    (symbol test
      (!even|five-to-ten :n ==> bool :o)
      (
        true @o
      )
    ) ::
    6 test
    11 test
    and 
  ) test.assert

  (
    {{1 :aa} :a 2 :b} :test1
    test1.b test1.a.aa + 3 ==
  ) test.assert

  (
    {} :obj
    3 :obj.three
    2 :obj.two
    (obj.three 6 <)
      (
        obj.three 1 + @obj.three
      )
    while
    obj.three 6 ==
  ) test.assert

  (
    {} :obj
    (dup *) ^square
    3 obj.square 9 ==
  ) test.assert

  ;; Numeric operations


  (2 2 + 4 ==) test.assert
  (1 3.0 + 4 ==) test.assert
  (3.1 3.9 + 7 ==) test.assert
  (3 -2.1 + 0.8999999999999999 ==) test.assert

  (3 3 - 0 ==) test.assert
  (-5 -4 - -1 ==) test.assert
  (-4 3.7 - -7.7 ==) test.assert

  (-2 4 * -8 ==) test.assert
  (-2.5 -2 * 5 ==) test.assert
  (3 3 * 9 ==) test.assert

  (5 2 / 2.5 ==) test.assert
  (1 3 / 0.3333333333333333 ==) test.assert
  (-3 2 / -1.5 ==) test.assert

  (5 2 div 2 ==) test.assert
  (1 3 div 0 ==) test.assert
  (-3 2 div -1 ==) test.assert
  
  (5 2 mod 1 ==) test.assert
  (4 2 mod 0 ==) test.assert
  (-3 2 mod -1 ==) test.assert

  (1000 random 1000 <) test.assert

  ((1 2 3 4 5) sum 15 ==) test.assert

  ((1 2 3 4 5) product 120 ==) test.assert

  ((1 2 3 4 5) avg 3.0 ==) test.assert
  ((1 2 3 4 5 6) avg 3.5 ==) test.assert

  ((1 3 5 7) med 4.0 ==) test.assert
  ((1 3 5 7 9) med 5 ==) test.assert

  ((1 5) range (1 2 3 4 5) ==) test.assert
  ((5 1) range (5 4 3 2 1) ==) test.assert
  ((4 7) range (4 5 6 7) ==) test.assert
  ((7 4) range (7 6 5 4) ==) test.assert
  ((1 6 2) range (1 3 5) ==) test.assert
  ((1 6 3) range (1 4) ==) test.assert
  ((0 6 2) range (0 2 4 6) ==) test.assert
  ((6 1 2) range (6 4 2) ==) test.assert
  ((6 1 3) range (6 3) ==) test.assert
  ((6 0 2) range (6 4 2 0) ==) test.assert
  
  (2 3 bitand 2 ==) test.assert

  (123 bitnot -124 ==) test.assert

  (2 3 bitor 3 ==) test.assert

  (2 3 bitxor 1 ==) test.assert

  (2 3 shl 16 ==) test.assert

  (16 3 shr 2 ==) test.assert
  
  (0 :c
  (c 10 <) (c succ @c) while
    c 10 ==) test.assert

  ((1 2 3 4 5) (even?) filter (2 4) ==) test.assert

  ((1 2 3 4 5) (even?) any?) test.assert

  ((2 4 6 8) (even?) all?) test.assert

  (base? "dec" ==) test.assert
  ('bin base "bin" base? == 'dec base) test.assert
  (0b10010 18 ==) test.assert
  (0b101010 0b010101 bitand 0b000000 ==) test.assert
  (0b101010 0b010101 bitor 0b111111 ==) test.assert
  (0b101010 0b010101 bitxor 0b111111 ==) test.assert
  (0b111000 (0 2) bitflip 0b111101 ==) test.assert
  (0b111001 (0) bitclear 0b111000 ==) test.assert
  (0b111000 (0 1) bitset 0b111011 ==) test.assert
  (0b111000 bitparity 1 ==) test.assert

  test.report
  ;; Tidy up
  clear-stack