mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 08:42:28 +00:00
Make redbean serialization deterministic
This commit is contained in:
parent
85aecbda67
commit
c9e68b0ebc
15 changed files with 452 additions and 150 deletions
58
test/libc/stdio/strlist_test.c
Normal file
58
test/libc/stdio/strlist_test.c
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/stdio/append.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/stdio/strlist.internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
struct StrList sl;
|
||||
|
||||
void TearDown(void) {
|
||||
FreeStrList(&sl);
|
||||
}
|
||||
|
||||
TEST(strlist, test) {
|
||||
int i;
|
||||
char *b = 0;
|
||||
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||
ASSERT_NE(-1, appends(&sl.p[i], "world"));
|
||||
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||
ASSERT_NE(-1, appends(&sl.p[i], "hello"));
|
||||
SortStrList(&sl);
|
||||
ASSERT_NE(-1, JoinStrList(&sl, &b, READ16LE(", ")));
|
||||
EXPECT_STREQ("hello, world", b);
|
||||
free(b);
|
||||
}
|
||||
|
||||
TEST(strlist, testNumbers) {
|
||||
int i;
|
||||
char *b = 0;
|
||||
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||
ASSERT_NE(-1, appends(&sl.p[i], "2"));
|
||||
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||
ASSERT_NE(-1, appends(&sl.p[i], "1"));
|
||||
SortStrList(&sl);
|
||||
ASSERT_NE(-1, JoinStrList(&sl, &b, ':'));
|
||||
EXPECT_STREQ("1:2", b);
|
||||
free(b);
|
||||
}
|
|
@ -54,22 +54,39 @@ assert(EncodeLatin1("helloÿÀ") == "hello\xff\xc0")
|
|||
assert(EncodeLua(nil) == "nil")
|
||||
assert(EncodeLua(0) == "0")
|
||||
assert(EncodeLua(3.14) == "3.14")
|
||||
assert(EncodeLua({1, 2}) == "{1, 2}")
|
||||
x = {1, 2}
|
||||
x[3] = x
|
||||
assert(string.match(EncodeLua(x), "{1, 2, \"cyclic@0x%x+\"}"))
|
||||
|
||||
-- TODO(jart): EncodeLua() should sort tables
|
||||
-- x = {}
|
||||
-- x.c = 'c'
|
||||
-- x.a = 'a'
|
||||
-- x.b = 'b'
|
||||
-- assert(EncodeLua(x) == '{a="a", b="b", c="c"}')
|
||||
assert(EncodeLua({2, 1}) == "{1, 2}")
|
||||
|
||||
assert(EncodeJson(nil) == "null")
|
||||
assert(EncodeJson(0) == "0")
|
||||
assert(EncodeJson(3.14) == "3.14")
|
||||
assert(EncodeJson({1, 2}) == "[1,2]")
|
||||
assert(EncodeJson({2, 1}) == "[1,2]")
|
||||
|
||||
-- EncodeLua() permits serialization of cyclic data structures
|
||||
x = {2, 1}
|
||||
x[3] = x
|
||||
assert(string.match(EncodeLua(x), "{\"cyclic@0x%x+\", 1, 2}"))
|
||||
|
||||
-- EncodeLua() sorts table entries
|
||||
x = {}
|
||||
x.c = 'c'
|
||||
x.a = 'a'
|
||||
x.b = 'b'
|
||||
assert(EncodeLua(x) == '{a="a", b="b", c="c"}')
|
||||
|
||||
-- EncodeJson() doesn't permit serialization of cyclic data structures
|
||||
x = {2, 1}
|
||||
x[3] = x
|
||||
json, err = EncodeJson(x)
|
||||
assert(not json)
|
||||
assert(err == "serialization failed")
|
||||
|
||||
-- EncodeJson() sorts table entries
|
||||
-- JSON always requires quotes around key names
|
||||
x = {}
|
||||
x.c = 'c'
|
||||
x.a = 'a'
|
||||
x.b = 'b'
|
||||
assert(EncodeJson(x) == '{"a":"a","b":"b","c":"c"}')
|
||||
|
||||
url = ParseUrl("https://jart:pass@redbean.dev/2.0.html?x&y=z#frag")
|
||||
assert(url.scheme == "https")
|
||||
|
@ -141,3 +158,17 @@ assert(Uncompress(Compress("hello")) == "hello")
|
|||
assert(Compress("hello") == "\x05\x86\xa6\x106x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15")
|
||||
assert(Compress("hello", 0) == "\x05\x86\xa6\x106x\x01\x01\x05\x00\xfa\xffhello\x06,\x02\x15")
|
||||
assert(Compress("hello", 0, true) == "x\x01\x01\x05\x00\xfa\xffhello\x06,\x02\x15")
|
||||
|
||||
----------------------------------------------------------------------------------------------------
|
||||
-- benchmarks
|
||||
|
||||
function LuaSerialization()
|
||||
EncodeLua({2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
function JsonSerialization()
|
||||
EncodeJson({2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
print(Benchmark(LuaSerialization), "LuaSerialization")
|
||||
print(Benchmark(JsonSerialization), "JsonSerialization")
|
||||
|
|
|
@ -140,8 +140,11 @@ function UnixTest()
|
|||
assert(unix.close(fd))
|
||||
|
||||
-- getdents
|
||||
t = {}
|
||||
for name, kind, ino, off in assert(unix.opendir(tmpdir)) do
|
||||
table.insert(t, name)
|
||||
end
|
||||
assert(EncodeLua(t) == '{".", "..", "foo"}');
|
||||
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue