_debug_info = 1; () = evalfile ("inc.sl"); testing_feature ("pack and unpack functions"); static variable is_lil_endian = (pack ("j", 0xFF)[0] == 0xFF); static define test_pack () { variable str; variable fmt, val, args; args = __pop_args (_NARGS - 2); (fmt, val) = (); str = pack (fmt, __push_args (args)); if (typeof (str) != BString_Type) failed ("pack did not return a bstring for format = " + fmt); if (str != val) failed ("pack returned wrong result for format = " + fmt + ":" + str); } variable X = 0x12345678L; variable S = "\x12\x34\x56\x78"; if (is_lil_endian) S = "\x78\x56\x34\x12"; test_pack (">k", "\x12\x34\x56\x78", X); test_pack ("j1", "\0\0A\0\0BC\0\xD\xE", 'A', ['B', 'C'], 0x0D0E); test_pack ("s4", "1234", "123456"); test_pack ("z4", "123\0", "123456"); test_pack ("S4", "1234", "123456"); test_pack ("s10", "1234\0\0\0\0\0\0", "1234"); test_pack ("S10", "1234 ", "1234"); define test_unpack1 (fmt, str, x, type) { variable xx; x = typecast (x, type); xx = unpack (fmt, str); if (length (where(xx != x))) failed ("unpack returned wrong result for " + fmt + ":" + string (xx)); } #ifexists Double_Type X = 3.14; if (X != unpack ("d", pack ("d", X))) failed ("pack->unpack for d"); X = 3.14f; if (X != unpack ("f", pack ("f", X))) failed ("pack->unpack for f"); #endif test_unpack1 (">j", "\xAB\xCD"B, 0xABCD, Int16_Type); test_unpack1 (">k", "\xAB\xCD\xEF\x12"B, 0xABCDEF12L, Int32_Type); test_unpack1 ("