"Fossies" - the Fresh Open Source Software Archive

Member "gawk-5.1.0/test/indirectbuiltin.awk" (6 Feb 2020, 7999 Bytes) of package /linux/misc/gawk-5.1.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) (G)AWK source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "indirectbuiltin.awk": 5.0.1_vs_5.1.0.

    1 function print_result(category, fname, builtin_result, indirect_result)
    2 {
    3     if (builtin_result == indirect_result)
    4         printf("%s: %s: pass\n", category, fname)
    5     else {
    6         printf("%s: %s: fail: builtin: %s \tindirect: %s\n", category, fname,
    7                 builtin_result, indirect_result)
    8         exit 1
    9     }
   10 }
   11 
   12 
   13 BEGIN {
   14 # math functions
   15 
   16     fun = "and"
   17     b1 = and(0x11, 0x01)
   18     i1 = @fun(0x11, 0x01)
   19     print_result("math", fun, b1, i1)
   20 
   21     fun = "atan2"
   22     b1 = atan2(-1, 0)
   23     i1 = @fun(-1, 0)
   24     print_result("math", fun, b1, i1)
   25 
   26     fun = "compl"
   27     b1 = compl(0x1111)
   28     i1 = @fun(0x1111)
   29     print_result("math", fun, b1, i1)
   30 
   31     fun = "cos"
   32     b1 = cos(3.1415927 / 4)
   33     i1 = @fun(3.1415927 / 4)
   34     print_result("math", fun, b1, i1)
   35 
   36     fun = "exp"
   37     b1 = exp(2)
   38     i1 = @fun(2)
   39     print_result("math", fun, b1, i1)
   40 
   41     fun = "int"
   42     b1 = int(3.1415927)
   43     i1 = @fun(3.1415927)
   44     print_result("math", fun, b1, i1)
   45 
   46     fun = "log"
   47     b1 = log(10)
   48     i1 = @fun(10)
   49     print_result("math", fun, b1, i1)
   50 
   51     fun = "lshift"
   52     b1 = lshift(1, 2)
   53     i1 = @fun(1, 2)
   54     print_result("math", fun, b1, i1)
   55 
   56     fun = "or"
   57     b1 = or(0x10, 0x01)
   58     i1 = @fun(0x10, 0x01)
   59     print_result("math", fun, b1, i1)
   60 
   61     fun = "rand"
   62     srand(1)
   63     b1 = rand();
   64     srand(1)
   65     i1 = @fun()
   66     print_result("math", fun, b1, i1)
   67 
   68     fun = "rshift"
   69     b1 = rshift(0x10, 1)
   70     i1 = @fun(0x10, 1)
   71     print_result("math", fun, b1, i1)
   72 
   73     fun = "sin"
   74     b1 = sin(3.1415927 / 4)
   75     i1 = @fun(3.1415927 / 4)
   76     print_result("math", fun, b1, i1)
   77 
   78     fun = "sqrt"
   79     b1 = sqrt(2)
   80     i1 = @fun(2)
   81     print_result("math", fun, b1, i1)
   82 
   83     srand()
   84     fun = "srand"
   85     b1 = srand()
   86     i1 = @fun()
   87     print_result("math", fun, b1, i1)
   88 
   89     fun = "xor"
   90     b1 = xor(0x11, 0x01)
   91     i1 = @fun(0x11, 0x01)
   92     print_result("math", fun, b1, i1)
   93 
   94 # string functions
   95 
   96     fun = "gensub"
   97     b1 = gensub("f", "q", "g", "ff11bb")
   98     i1 = @fun("f", "q", "g", "ff11bb")
   99     print_result("string", fun, b1, i1)
  100 
  101     fun = "gensub"
  102     $0 = "ff11bb"
  103     b1 = gensub("f", "q", "g")
  104     i1 = @fun("f", "q", "g")
  105     print_result("string", fun " (3 args)", b1, i1)
  106 
  107     fun = "gsub"
  108     $0 = "ff11bb"
  109     b1 = gsub("f", "q")
  110     b2 = $0
  111     $0 = "ff11bb"
  112     i1 = @fun("f", "q")
  113     i2 = $0
  114     print_result("string", fun, b1, i1)
  115     if (b2 != i2) {
  116         printf("string: %s: fail: $0 (%s) != $0 (%s)\n",
  117             fun, b2, i2)
  118         exit 1
  119     }
  120 
  121     fun = "index"
  122     b1 = index("hi, how are you", "how")
  123     i1 = @fun("hi, how are you", "how")
  124     print_result("string", fun, b1, i1)
  125 
  126     fun = "dcgettext"
  127     b1 = dcgettext("hello, world")
  128     i1 = @fun("hello, world")
  129     print_result("string", fun, b1, i1)
  130 
  131     fun = "dcngettext"
  132     b1 = dcngettext("hello, world", "howdy", 2)
  133     i1 = @fun("hello, world", "howdy", 2)
  134     print_result("string", fun, b1, i1)
  135 
  136     fun = "length"
  137     b1 = length("hi, how are you")
  138     i1 = @fun("hi, how are you")
  139     print_result("string", fun, b1, i1)
  140 
  141     fun = "sprintf"
  142     b1 = sprintf("%s world", "hello")
  143     i1 = @fun("%s world", "hello")
  144     print_result("string", fun, b1, i1)
  145 
  146     fun = "strtonum"
  147     b1 = strtonum("0xdeadbeef")
  148     i1 = @fun("0xdeadbeef")
  149     print_result("string", fun, b1, i1)
  150 
  151     fun = "sub"
  152     $0 = "ff11bb"
  153     b1 = sub("f", "q")
  154     b2 = $0
  155     $0 = "ff11bb"
  156     i1 = @fun("f", "q")
  157     i2 = $0
  158     print_result("string", fun, b1, i1)
  159     if (b2 != i2) {
  160         printf("string: %s: fail: $0 (%s) != $0 (%s)\n",
  161             fun, b2, i2)
  162         exit 1
  163     }
  164 
  165     fun = "substr"
  166     b1 = substr("0xdeadbeef", 7, 4)
  167     i1 = @fun("0xdeadbeef", 7, 4)
  168     print_result("string", fun, b1, i1)
  169 
  170     fun = "tolower"
  171     b1 = tolower("0xDeAdBeEf")
  172     i1 = @fun("0xDeAdBeEf")
  173     print_result("string", fun, b1, i1)
  174 
  175     fun = "toupper"
  176     b1 = toupper("0xDeAdBeEf")
  177     i1 = @fun("0xDeAdBeEf")
  178     print_result("string", fun, b1, i1)
  179 
  180 # time functions
  181 
  182     fun = "mktime"
  183     b1 = mktime("1990 02 11 12 00 00")
  184     i1 = @fun("1990 02 11 12 00 00")
  185     print_result("time", fun, b1, i1)
  186 
  187     then = b1
  188     fun = "strftime"
  189     b1 = strftime(PROCINFO["strftime"], then)
  190     i1 = @fun(PROCINFO["strftime"], then)
  191     print_result("time", fun, b1, i1)
  192 
  193     fun = "systime"
  194     b1 = systime()
  195     i1 = @fun()
  196     print_result("time", fun, b1, i1)
  197 
  198 # regexp functions
  199 
  200     fun = "match"
  201     b1 = match("fooob", "o+")
  202     rstart = RSTART
  203     rlength = RLENGTH
  204     i1 = @fun("fooob", "o+")
  205     print_result("regexp", fun, b1, i1)
  206     if (rstart != RSTART) {
  207         printf("match: failure: biRSTART (%d) != iRSTART (%d)\n",
  208             rstart, RSTART)
  209         exit 1
  210     }
  211     if (rlength != RLENGTH) {
  212         printf("match: failure: biRLENGTH (%d) != iRLENGTH (%d)\n",
  213             rlength, RLENGTH)
  214         exit 1
  215     }
  216 
  217     ############## start patsplit ##############
  218     fun = "patsplit"
  219     delete data
  220     delete data2
  221     delete seps
  222     delete seps2
  223     b1 = patsplit("a:b:c:d", data, ":", seps)
  224     i1 = @fun("a:b:c:d", data2, ":", seps2)
  225     print_result("regexp", fun, b1, i1)
  226     for (i in data) {
  227         if ((! (i in data2)) || data[i] != data2[i]) {
  228             printf("patsplit1a: fail: builtin data[%d] (%s) != indirect data[%d] (%s)\n",
  229                 i, data[i], i, data2[i])
  230             exit 1
  231         }
  232     }
  233     for (i in seps) {
  234         if ((! (i in seps2)) || seps[i] != seps2[i]) {
  235             printf("patsplit1b: fail: builtin seps[%d] (%s) != indirect seps[%d] (%s)\n",
  236                 i, seps[i], i, seps2[i])
  237             exit 1
  238         }
  239     }
  240 
  241     fun = "patsplit"
  242     delete data
  243     delete data2
  244     b1 = patsplit("a:b:c:d", data, ":")
  245     i1 = @fun("a:b:c:d", data2, ":")
  246     print_result("regexp", fun, b1, i1)
  247     for (i in data) {
  248         if ((! (i in data2)) || data[i] != data2[i]) {
  249             printf("patsplit2: fail: builtin data[%d] (%s) != indirect data[%d] (%s)\n",
  250                 i, data[i], i, data2[i])
  251             exit 1
  252         }
  253     }
  254 
  255     fun = "patsplit"
  256     delete data
  257     delete data2
  258     FPAT = "[a-z]+"
  259     b1 = patsplit("a b c d", data)
  260     i1 = @fun("a b c d", data2)
  261     print_result("regexp", fun, b1, i1)
  262     for (i in data) {
  263         if ((! (i in data2)) || data[i] != data2[i]) {
  264             printf("patsplit3: fail: builtin data[%d] (%s) != indirect data[%d] (%s)\n",
  265                 i, data[i], i, data2[i])
  266             exit 1
  267         }
  268     }
  269     ############## end patsplit ##############
  270 
  271     ############## start split ##############
  272     fun = "split"
  273     delete data
  274     delete data2
  275     delete seps
  276     delete seps2
  277     b1 = split("a:b:c:d", data, ":", seps)
  278     i1 = @fun("a:b:c:d", data2, ":", seps2)
  279     print_result("regexp", fun, b1, i1)
  280     for (i in data) {
  281         if ((! (i in data2)) || data[i] != data2[i]) {
  282             printf("split1a: fail: builtin data[%d] (%s) != indirect data[%d] (%s)\n",
  283                 i, data[i], i, data2[i])
  284             exit 1
  285         }
  286     }
  287     for (i in seps) {
  288         if ((! (i in seps2)) || seps[i] != seps2[i]) {
  289             printf("split1b: fail: builtin seps[%d] (%s) != indirect seps[%d] (%s)\n",
  290                 i, seps[i], i, seps2[i])
  291             exit 1
  292         }
  293     }
  294 
  295     fun = "split"
  296     delete data
  297     delete data2
  298     b1 = split("a:b:c:d", data, ":")
  299     i1 = @fun("a:b:c:d", data2, ":")
  300     print_result("regexp", fun, b1, i1)
  301     for (i in data) {
  302         if ((! (i in data2)) || data[i] != data2[i]) {
  303             printf("split2: fail: builtin data[%d] (%s) != indirect data[%d] (%s)\n",
  304                 i, data[i], i, data2[i])
  305             exit 1
  306         }
  307     }
  308 
  309     fun = "split"
  310     delete data
  311     delete data2
  312     b1 = split("a b c d", data)
  313     i1 = @fun("a b c d", data2)
  314     print_result("regexp", fun, b1, i1)
  315     for (i in data) {
  316         if ((! (i in data2)) || data[i] != data2[i]) {
  317             printf("split3: fail: builtin data[%d] (%s) != indirect data[%d] (%s)\n",
  318                 i, data[i], i, data2[i])
  319             exit 1
  320         }
  321     }
  322     ############## end split ##############
  323 
  324 # array functions
  325 
  326     split("z y x w v u t", data)
  327     fun = "asort"
  328     asort(data, newdata)
  329     @fun(data, newdata2)
  330     print_result("array", fun, b1, i1)
  331     for (i in newdata) {
  332         if (! (i in newdata2) || newdata[i] != newdata2[i]) {
  333             print fun ": failed, index", i
  334             exit
  335         }
  336     }
  337 
  338     for (i in data)
  339         data2[data[i]] = i
  340 
  341     fun = "asorti"
  342     asorti(data2, newdata)
  343     @fun(data2, newdata2)
  344     print_result("array", fun, b1, i1)
  345     for (i in newdata) {
  346         if (! (i in newdata2) || newdata[i] != newdata2[i]) {
  347             print fun ": failed, index", i, "value", newdata[i], newdata2[i]
  348             exit
  349         }
  350     }
  351 
  352     arr[1] = arr[2] = 42
  353     fun = "isarray"
  354     b1 = isarray(arr)
  355     i1 = @fun(arr)
  356     print_result("array", fun, b1, i1)
  357 
  358 # i/o functions
  359 
  360     print("hi") > "x1.out"
  361     print("hi") > "x2.out"
  362 
  363     fun = "fflush"
  364     b1 = fflush("x1.out")
  365     i1 = @fun("x2.out")
  366     print_result("i/o", fun, b1, i1)
  367 
  368     fun = "close"
  369     b1 = close("x1.out")
  370     i1 = @fun("x2.out")
  371     print_result("i/o", fun, b1, i1)
  372 
  373     fun = "system"
  374     b1 = system("rm x1.out")
  375     i1 = @fun("rm x2.out")
  376     print_result("i/o", fun, b1, i1)
  377 }