quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

test_rw_handler.js (1441B)


      1 import * as std from "std";
      2 import * as os from "os";
      3 
      4 function assert(actual, expected, message) {
      5     if (arguments.length == 1)
      6         expected = true;
      7 
      8     if (Object.is(actual, expected))
      9         return;
     10 
     11     if (actual !== null && expected !== null
     12     &&  typeof actual == 'object' && typeof expected == 'object'
     13     &&  actual.toString() === expected.toString())
     14         return;
     15 
     16     throw Error("assertion failed: got |" + actual + "|" +
     17                 ", expected |" + expected + "|" +
     18                 (message ? " (" + message + ")" : ""));
     19 }
     20 
     21 function handle_read(fd_r, fd_w, i)
     22 {
     23     var buf = new Uint32Array(1);
     24     var val, len;
     25     len = os.read(fd_r, buf.buffer, 0, 4);
     26     os.setReadHandler(fd_r, null);
     27     val = buf[0];
     28 //    print("read fd=", fd_r, "val=", val, "len=", len);
     29     assert(val, i);
     30 }
     31 
     32 function handle_write(fd_r, fd_w, i)
     33 {
     34     var buf = new Uint32Array(1);
     35     buf[0] = i;
     36     os.write(fd_w, buf.buffer, 0, 4);
     37     os.setWriteHandler(fd_w, null);
     38 }
     39 
     40 function test_rw_handlers(n)
     41 {
     42     var tab, fd_r, fd_w, i;
     43     tab = [];
     44     for(i = 0; i < n; i++) {
     45         tab[i] = os.pipe();
     46         fd_r = tab[i][0];
     47         fd_w = tab[i][1];
     48         os.setReadHandler(fd_r, handle_read.bind(null, fd_r, fd_w, i));
     49     }
     50     for(i = n - 1; i >= 0; i--) {
     51         fd_r = tab[i][0];
     52         fd_w = tab[i][1];
     53         os.setWriteHandler(fd_w, handle_write.bind(null, fd_r, fd_w, i));
     54     }
     55 }
     56 
     57 test_rw_handlers(100);