busybox/testsuite/bc.tests
<<
>>
Prefs
   1#!/bin/sh
   2# Copyright 2018 by Denys Vlasenko
   3# Licensed under GPLv2 or later, see file LICENSE in this source tree.
   4
   5. ./testing.sh
   6
   7# testing "test name" "command" "expected result" "file input" "stdin"
   8
   9testing "bc comment" \
  10        "bc" \
  11        "3\n" \
  12        "" "1 /* comment */ + 2"
  13
  14testing "bc /*/ is not a closed comment" \
  15        "bc" \
  16        "4\n" \
  17        "" "1 /*/ + 2 */ + 3"
  18
  19# this needs interactive testing
  20testing "bc comment with \"" \
  21        "bc" \
  22        "3\n" \
  23        "" "1 /* \" */ + 2"
  24
  25# this needs interactive testing
  26testing "bc \"string/*\" is not a comment" \
  27        "bc" \
  28        "string/*9\n" \
  29        "" "\"string/*\";9"
  30
  31testing "bc comment 3: unterminated #comment" \
  32        "bc" \
  33        "" \
  34        "" "#foo"  # no trailing newline
  35
  36testing "bc backslash 1" \
  37        "bc" \
  38        "3\n" \
  39        "" "1 \\\\\n + 2"
  40
  41testing "bc string 1" \
  42        "bc" \
  43        "STR\n" \
  44        "" "\"STR\n\""
  45
  46testing "bc read() 4<EOF>" \
  47        "bc input" \
  48        "4\n" \
  49        "read();halt" "4"
  50
  51testing "bc read()^2" \
  52        "bc input" \
  53        "16\n" \
  54        "read()^2;halt" "4\n"
  55
  56testing "bc read()*read()" \
  57        "bc input" \
  58        "20\n" \
  59        "read()*read();halt" "4\n5"
  60
  61testing "bc if 0 else" \
  62        "bc" \
  63        "2\n9\n" \
  64        "" "if (0) 1 else 2; 9"
  65
  66testing "bc if 1 else" \
  67        "bc" \
  68        "1\n9\n" \
  69        "" "if (1) 1 else 2; 9"
  70
  71testing "bc if 1 if 1 else else" \
  72        "bc" \
  73        "1\n9\n" \
  74        "" "if (1) if (1) 1 else 2 else 3; 9"
  75
  76testing "bc if 0 else if 1" \
  77        "bc" \
  78        "2\n9\n" \
  79        "" "if (0) 1 else if (1) 2; 9"
  80
  81testing "bc for (;;)" \
  82        "bc" \
  83        "2\n3\n2\n9\n" \
  84        "" "i=2; for (;;) { 2; if(--i==0) break; 3; }; 9"
  85
  86testing "bc for (;cond;)" \
  87        "bc" \
  88        "1\n2\n3\n9\n" \
  89        "" "i=0; for(;i<3;)++i; 9"
  90
  91testing "bc for (;cond;upd)" \
  92        "bc" \
  93        "1\n2\n3\n9\n" \
  94        "" "i=1; for(;i<4;i++)i; 9"
  95
  96testing "bc for (init;cond;upd)" \
  97        "bc" \
  98        "1\n2\n3\n9\n" \
  99        "" "for(i=1;i<4;i++)i; 9"
 100
 101testing "bc for (;;) {break}" \
 102        "bc" \
 103        "2\n9\n" \
 104        "" "for (;;) {2;break}; 9"
 105
 106testing "bc define {return}" \
 107        "bc" \
 108        "0\n9\n" \
 109        "" "define w() {return}\nw();9"
 110
 111testing "bc define auto" \
 112        "bc" \
 113        "8\n9\n" \
 114        "" "define w() { auto z; return 8; }; w(); 9"
 115
 116testing "bc define auto array same name" \
 117        "bc" \
 118        "8\n9\n" \
 119        "" "define w(x) { auto x[]; return x; }; w(8); 9"
 120
 121testing "bc define with body on next line" \
 122        "bc" \
 123        "8\n9\n" \
 124        "" "define w()\n{ auto z; return 8; }\nw()\n9"
 125
 126testing "bc void function" \
 127        "bc" \
 128        "void9\n" \
 129        "" "define void w() {print \"void\"}\nw()\n9"
 130
 131# Extra POSIX compat - GNU bc does not allow this
 132testing "bc function named 'void'" \
 133        "bc" \
 134        "void0\n9\n" \
 135        "" "define void() {print \"void\"}\nvoid()\n9"
 136
 137# Extra POSIX compat - GNU bc does not allow this
 138testing "bc variable named 'void'" \
 139        "bc" \
 140        "6\n9\n" \
 141        "" "void=6\nvoid\n9"
 142
 143testing "bc if(cond)<NL>" \
 144        "bc" \
 145        "9\n" \
 146        "" "if(0)\n3\n9"
 147
 148testing "bc if(cond) stmt else<NL>" \
 149        "bc" \
 150        "4\n9\n" \
 151        "" "if(0)3 else\n4\n9"
 152
 153testing "bc while(cond)<NL>" \
 154        "bc" \
 155        "8\n7\n6\n5\n4\n3\n2\n1\n9\n" \
 156        "" "i=9;while(--i)\ni\n9"
 157
 158testing "bc ifz does not match if keyword" \
 159        "bc" \
 160        "1\n2\n2\n3\n" \
 161        "" "ifz=1;ifz\n++ifz;ifz++\nifz"
 162
 163# had parse error on "f()-N"
 164testing "bc -l 'e(0)-2'" \
 165        "bc -l" \
 166        "-1.00000000000000000000\n" \
 167        "" "e(0)-2"
 168
 169testing "bc (!a&&b)" \
 170        "bc" \
 171        "0\n" \
 172        "" "(!a&&b)"
 173
 174# check that dc code is not messing this up (no NUL printing!)
 175testing "bc print \"\"" \
 176        "bc" \
 177        "" \
 178        "" "print \"\""
 179
 180testing "bc print 1,2,3" \
 181        "bc" \
 182        "123" \
 183        "" "print 1,2,3"
 184
 185testing "bc length" \
 186        "bc" \
 187        "1\n3\n1\n3\n3\n" \
 188        "" "length(0); length(100); length(0.01); length(0.00120); length(0.012-0.012);"
 189
 190testing "bc { print 1 }" \
 191        "bc" \
 192        "1" \
 193        "" "{ print 1 }"
 194
 195testing "bc comparison 1" \
 196        "bc" \
 197        "1\n" \
 198        "" "-10 < -9"
 199
 200testing "bc nested loops and breaks" \
 201        "bc" \
 202        "\
 20311
 20421
 20531
 20622
 20712
 20899
 209" \
 210        "" "\
 211if(1) {
 212        11
 213        while(1) {
 214                21
 215                while(1) {
 216                        31
 217                        break
 218                        32
 219                }
 220                22
 221                break
 222                23
 223        }
 224        12
 225} else {
 226        88
 227}
 22899
 229"
 230
 231testing "bc continue in if" \
 232        "bc" \
 233        "\
 23411
 23521
 23611
 23731
 23899
 239" \
 240        "" "\
 241i=2
 242while(i--) {
 243        11
 244        if(i) {
 245                21
 246                continue
 247                22
 248        } else {
 249                31
 250                continue
 251        32
 252        }
 253        12
 254}
 25599
 256"
 257
 258testing "bc continue in for" \
 259        "bc" \
 260        "\
 2611
 26277
 2632
 26499
 265" \
 266        "" "\
 267for(i=1; i<3; i++) {
 268    i
 269    if(i==2) continue
 270    77
 271}
 27299
 273"
 274
 275testing "bc ibase" \
 276        "bc" \
 277        "99\n1295\n1224\n" \
 278        "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a"
 279
 280testing "bc parsing of numbers" \
 281        "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
 282        "465d8c01308d0863b6f5669e8a1c69fb  -\n" \
 283        "" '
 284for (b = 2; b <= 16; ++b) {
 285        if (b == 10) continue
 286        obase = 10
 287        print "ibase = A; ibase = ", b, "\n"
 288        obase = b
 289        for (i = 0; i <= 65536; ++i) {
 290                i
 291                print "0.", i, "\n"
 292                print "1.", i, "\n"
 293                print i, ".", i, "\n"
 294        }
 295}'
 296
 297testing "bc printing of numbers" \
 298        "bc 2>&1 | bc 2>&1 | md5sum 2>&1" \
 299        "d884b35d251ca096410712743aeafb9e  -\n" \
 300        "" '
 301for (b = 2; b <= 101; ++b) {
 302        if (b == 10) continue
 303        s = b * b
 304        print "obase = ", b, "\n"
 305        for (i = 0; i <= s; ++i) {
 306                i
 307                print "0.", i, "\n"
 308                print "1.", i, "\n"
 309                print i, ".", i, "\n"
 310        }
 311        2189432174861923048671023498128347619023487610234689172304.192748960128745108927461089237469018723460
 312}'
 313
 314for f in bc*.bc; do
 315        r="`basename "$f" .bc`_results.txt"
 316        test -f "$r" || continue
 317        # testing "test name" "command" "expected result" "file input" "stdin"
 318        testing "bc -lq $f" \
 319                "{ { bc -lq $f 2>&1; echo E:\$? >&2; } | diff -u - $r; echo E:\$?; } 2>&1" \
 320                "E:0\nE:0\n" \
 321                "" ""
 322done
 323
 324exit $FAILCOUNT
 325