. "$BST_LIB" require bitwise bitwise/present # bitwise prints ANSI colour unconditionally -- g_has_color defaults to 1 and # init_colors() never consults isatty() -- so every value assertion below runs # with --no-color. bitwise/color-on covers the default the other way round. bw() { bitwise --no-color "$@"; } check_out bitwise/version "bitwise v0.50" bitwise --version check_out bitwise/help "-w, --width[b|w|l|d]" bitwise --help # --- conversion, default width (auto-sized from the value: 255 -> 8 bits) --- check_out bitwise/dec "Unsigned decimal: 255" bw 255 check_out bitwise/signed8 "Signed decimal: -1" bw 255 check_out bitwise/hex "Hexadecimal: 0xff" bw 255 check_out bitwise/oct "Octal: 0377" bw 255 check_out bitwise/binary "1 1 1 1 1 1 1 1" bw 255 check_out bitwise/ruler8 "7 - 0" bw 255 # --- input radices: the parser's four entry points --- check_out bitwise/in-hex "Unsigned decimal: 65" bw 0x41 check_out bitwise/in-bin "Unsigned decimal: 11" bw 0b1011 check_out bitwise/in-oct "Unsigned decimal: 8" bw 010 check_out bitwise/in-ipv4 "Unsigned decimal: 16885952" bw 192.168.1.1 # --- explicit widths. -w d is the 64-bit arm: a wasm32 target computing and # printing full 64-bit quantities, which is the arithmetic worth pinning here. check_out bitwise/width-w "Hexadecimal: 0x1234" bw -w w 4660 check_out bitwise/width-w-ruler "15 - 8" bw -w w 4660 check_out bitwise/width-d-uns "Unsigned decimal: 16045690984503098046" bw -w d 0xdeadbeefcafebabe check_out bitwise/width-d-sgn "Signed decimal: -2401053089206453570" bw -w d 0xdeadbeefcafebabe check_out bitwise/width-d-oct "Octal: 01572555756771277535276" bw -w d 0xdeadbeefcafebabe check_rc bitwise/width-bad 1 bitwise --no-color -w q 5 # Asserted by hand rather than with check_out: these two commands are SUPPOSED to # exit non-zero (check_rc above is the other half of the same assertion), and # check_out requires exit 0 as well as the substring -- so it would report a red # for the very behaviour being asserted. out=$(bitwise --no-color -w q 5 2>&1) case "$out" in *"Unsupported width size"*) bst_ok bitwise/width-bad-msg ;; *) bst_notok bitwise/width-bad-msg "$(_bst_1line "$out")" ;; esac # --- shunting-yard expression evaluator --- check_out bitwise/expr-prec "Unsigned decimal: 7" bw '1 + 2 * 3' check_out bitwise/expr-and "Unsigned decimal: 15" bw '0xff & 0x0f' check_out bitwise/expr-or "Unsigned decimal: 255" bw '0xf0 | 0x0f' check_out bitwise/expr-shl "Unsigned decimal: 1024" bw '1 << 10' check_out bitwise/expr-shr "Unsigned decimal: 16" bw '1024 >> 6' check_out bitwise/expr-not "Hexadecimal: 0xffffffffffffffff" bw '~0' check_rc bitwise/expr-bad 1 bitwise --no-color notanumber out=$(bitwise --no-color notanumber 2>&1) case "$out" in *"Couldn't parse expression"*) bst_ok bitwise/expr-bad-msg ;; *) bst_notok bitwise/expr-bad-msg "$(_bst_1line "$out")" ;; esac # --- human-readable size: IEC by default, SI under -s --- check_out bitwise/human-iec "Human: 1.00 MiB" bw 1048576 check_out bitwise/human-si "Human: 1.00 MB" bw -s 1000000 check_out bitwise/human-kib "Human: 1.00 KiB" bw 1024 # --- the glibc surface this tool reaches for and little else on the image does. # l64a() is the legacy radix-64 encoder; inet_ntoa() formats the address; the # little-endian line is bswap_32() through it. All three are wasm32 glibc code # paths with no other consumer in brintos-selftests. check_out bitwise/radix64 "Radix64: .E" bw 1024 check_out bitwise/ipv4-big "IPv4 (Network byte order - Big): 255.0.0.0" bw 255 check_out bitwise/ipv4-little "IPv4 (Reversed byte order - Little): 0.0.0.255" bw 255 check_out bitwise/ipv4-toobig "Value too big to be a valid IPv4 address" bw -w d 0xdeadbeefcafebabe # isgraph() decides printable-vs-dot per byte; 0x41 is the only graphic byte here check_out bitwise/ascii "ASCII: .......A" bw 0x41 # Radix64 is emitted only below 64 bits (l64a takes a long); at -w d it is absent. out=$(bw -w d 0xdeadbeefcafebabe 2>&1) case "$out" in *Radix64*) bst_notok bitwise/radix64-off-at-64 "Radix64 printed at width 64" ;; *) bst_ok bitwise/radix64-off-at-64 ;; esac # --- overflow is reported, then the value is masked to the width --- check_out bitwise/overflow-msg "Expression overflowed!" bw -w b 300 check_out bitwise/overflow-wrap "Unsigned decimal: 44" bw -w b 300 # --- colour is on unless --no-color: assert the raw SGR bytes, not a rendering --- esc=$(printf '\033') out=$(bitwise 42 2>&1) case "$out" in *"$esc[32m"*) bst_ok bitwise/color-on ;; *) bst_notok bitwise/color-on "no SGR green in default output" ;; esac case "$(bw 42 2>&1)" in *"$esc["*) bst_notok bitwise/color-off "SGR bytes survived --no-color" ;; *) bst_ok bitwise/color-off ;; esac # --- the interactive half: ncurses full-screen UI + the readline command line. # Driven through a real pty by data/selftest_bitwise.exp, because neither # initscr() nor readline() does anything meaningful on a pipe. if command -v expect >/dev/null 2>&1; then out=$(expect -f "$BST_DATA/selftest_bitwise.exp" 2>&1); rc=$? for c in tui-render tui-quit-key tui-width-key tui-readline-quit; do case "$out" in *"BWX-OK $c"*) bst_ok "bitwise/$c" ;; *"BWX-FAIL $c"*) bst_notok "bitwise/$c" "$(printf '%s\n' "$out" | grep -m1 "BWX-FAIL $c" | cut -c1-100)" ;; *) bst_notok "bitwise/$c" "no verdict (rc=$rc) $(_bst_1line "$out")" ;; esac done else for c in tui-render tui-quit-key tui-width-key tui-readline-quit; do bst_skip "bitwise/$c" "expect absent — pty coverage unavailable" done fi