Add symbol comparison and test for it
This commit is contained in:
parent
7bcd6afeb2
commit
ae6f86e4f7
3 changed files with 51 additions and 1 deletions
|
@ -48,6 +48,7 @@ enable_testing()
|
|||
set(CPP_TESTS
|
||||
dict
|
||||
array
|
||||
symbol
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -480,8 +480,26 @@ class Symbol : public Object {
|
|||
virtual Result<short> cmp(Arena& arena, Object& rhs) final {
|
||||
return -TRY(rhs.cmp(arena, *this));
|
||||
}
|
||||
virtual Result<short> cmp(Arena&, Symbol& rhs) final { return 0; }
|
||||
virtual Result<short> cmp(Arena&, Symbol& rhs) final {
|
||||
auto lsize = size();
|
||||
auto rsize = rhs.size();
|
||||
uint64_t i = 0;
|
||||
uint64_t j = 0;
|
||||
while (1) {
|
||||
if (i == lsize && j == lsize) return 0;
|
||||
|
||||
short cmp = short(i == lsize) - short(j == rsize);
|
||||
if (cmp != 0) return cmp;
|
||||
|
||||
char32_t lc = _value->data[i];
|
||||
char32_t rc = rhs._value->data[j];
|
||||
cmp = (lc > rc) - (lc < rc);
|
||||
if (cmp != 0) return cmp;
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
virtual void move(Object* obj) final { new (obj) Symbol(std::move(_value)); }
|
||||
|
||||
static Result<Symbol> create(Arena& arena, PodSymbol* obj) {
|
||||
|
@ -497,6 +515,17 @@ class Symbol : public Object {
|
|||
return Symbol(TRY(MkGcRoot(pod, arena)));
|
||||
}
|
||||
|
||||
static Result<Symbol> create(Arena& arena, const char* str) {
|
||||
uint64_t size = strlen(str);
|
||||
auto pod = TRY(arena.alloc<PodSymbol>(size * sizeof(char32_t)));
|
||||
pod->header.tag = Tag::Symbol;
|
||||
|
||||
for (uint64_t i = 0; i < size; i++) pod->data[i] = str[i];
|
||||
pod->size = size;
|
||||
|
||||
return Symbol(TRY(MkGcRoot(pod, arena)));
|
||||
}
|
||||
|
||||
static Result<Symbol> create(Arena& arena, String& rhs);
|
||||
virtual Result<Value> copy(Arena& arena) const final;
|
||||
|
||||
|
|
20
test/symbol.cpp
Normal file
20
test/symbol.cpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "common.hpp"
|
||||
#include "die.hpp"
|
||||
#include "reader.hpp"
|
||||
#include "test.hpp"
|
||||
#include "writer.hpp"
|
||||
|
||||
StaticArena<64 * 1024 * 1024> arena;
|
||||
|
||||
TEST_CASE(array_append) {
|
||||
auto a = DIEX(Symbol::create(arena, "foo"));
|
||||
auto b = DIEX(Symbol::create(arena, "foo"));
|
||||
|
||||
ASSERT_EQUALS(a, b);
|
||||
|
||||
auto s = DIEX(write_one(arena, a));
|
||||
|
||||
DIEX(arena.gc());
|
||||
|
||||
ASSERT_EQUALS(s, "foo");
|
||||
}
|
Loading…
Reference in a new issue