static void test_Dump_and_Load(TestBatch *batch) { Hash *hash = Hash_new(0); Obj *dump; Hash *loaded; Hash_Store_Str(hash, "foo", 3, (Obj*)CB_new_from_trusted_utf8("foo", 3)); dump = (Obj*)Hash_Dump(hash); loaded = (Hash*)Obj_Load(dump, dump); TEST_TRUE(batch, Hash_Equals(hash, (Obj*)loaded), "Dump => Load round trip"); DECREF(dump); DECREF(loaded); /* TODO: Fix Hash_Load(). Hash_Store_Str(hash, "_class", 6, (Obj*)CB_new_from_trusted_utf8("not_a_class", 11)); dump = (Obj*)Hash_Dump(hash); loaded = (Hash*)Obj_Load(dump, dump); TEST_TRUE(batch, Hash_Equals(hash, (Obj*)loaded), "Load still works with _class if it's not a real class"); DECREF(dump); DECREF(loaded); */ DECREF(hash); }
bool Doc_Equals_IMP(Doc *self, Obj *other) { if ((Doc*)other == self) { return true; } if (!Obj_Is_A(other, DOC)) { return false; } DocIVARS *const ivars = Doc_IVARS(self); DocIVARS *const ovars = Doc_IVARS((Doc*)other); return Hash_Equals((Hash*)ivars->fields, (Obj*)ovars->fields); }
int Node_Same(Node *a, Node *b) { assert(a != NULL && "NULL Node pointer"); assert(b != NULL && "NULL Node pointer"); return a->addr.s_addr == b->addr.s_addr && a->port == b->port && Hash_Equals(&a->id, &b->id); }
bool Schema_Equals_IMP(Schema *self, Obj *other) { if ((Schema*)other == self) { return true; } if (!Obj_is_a(other, SCHEMA)) { return false; } SchemaIVARS *const ivars = Schema_IVARS(self); SchemaIVARS *const ovars = Schema_IVARS((Schema*)other); if (!Arch_Equals(ivars->arch, (Obj*)ovars->arch)) { return false; } if (!Sim_Equals(ivars->sim, (Obj*)ovars->sim)) { return false; } if (!Hash_Equals(ivars->types, (Obj*)ovars->types)) { return false; } return true; }
bool_t Stopalizer_equals(Stopalizer *self, Obj *other) { Stopalizer *const evil_twin = (Stopalizer*)other; if (evil_twin == self) return true; if (!Obj_Is_A(other, STOPALIZER)) return false; if (!Hash_Equals(evil_twin->stoplist, (Obj*)self->stoplist)) { return false; } return true; }
bool SnowStop_Equals_IMP(SnowballStopFilter *self, Obj *other) { if ((SnowballStopFilter*)other == self) { return true; } if (!Obj_Is_A(other, SNOWBALLSTOPFILTER)) { return false; } SnowballStopFilterIVARS *const ivars = SnowStop_IVARS(self); SnowballStopFilterIVARS *const ovars = SnowStop_IVARS((SnowballStopFilter*)other); if (!Hash_Equals(ivars->stoplist, (Obj*)ovars->stoplist)) { return false; } return true; }
static void test_Equals(TestBatch *batch) { Hash *hash = Hash_new(0); Hash *other = Hash_new(0); ZombieCharBuf *stuff = ZCB_WRAP_STR("stuff", 5); TEST_TRUE(batch, Hash_Equals(hash, (Obj*)other), "Empty hashes are equal"); Hash_Store_Str(hash, "foo", 3, (Obj*)CFISH_TRUE); TEST_FALSE(batch, Hash_Equals(hash, (Obj*)other), "Add one pair and Equals returns false"); Hash_Store_Str(other, "foo", 3, (Obj*)CFISH_TRUE); TEST_TRUE(batch, Hash_Equals(hash, (Obj*)other), "Add a matching pair and Equals returns true"); Hash_Store_Str(other, "foo", 3, INCREF(stuff)); TEST_FALSE(batch, Hash_Equals(hash, (Obj*)other), "Non-matching value spoils Equals"); DECREF(hash); DECREF(other); }
int DecodeResponse(Message *message, BNode *dict, struct PendingResponses *pending) { assert(message != NULL && "NULL Message pointer"); assert(dict != NULL && "NULL BNode pointer"); assert(dict->type == BDictionary && "Not a dictionary"); assert(pending != NULL && "NULL struct PendingResponses pointer"); int rc = SetTransactionId(message, dict); check(rc == 0, "SetTransactionId failed"); if (message->t_len != sizeof(tid_t)) { message->errors |= MERROR_INVALID_TID; } SetResponseId(message, dict); PendingResponse entry = pending->getPendingResponse(pending, message->t, &rc); if (rc == 0) { if (!Hash_Equals(&message->id, &entry.id) && !entry.is_new) { message->errors |= MERROR_INVALID_NODE_ID; } message->type = entry.type; message->context = entry.context; } else { message->errors |= MERROR_INVALID_TID; } rc = SetResponseData(message, dict); check(rc == 0, "SetResponseData failed"); return 0; error: return -1; }
static void test_Store_and_Fetch(TestBatch *batch) { Hash *hash = Hash_new(100); Hash *dupe = Hash_new(100); const uint32_t starting_cap = Hash_Get_Capacity(hash); VArray *expected = VA_new(100); VArray *got = VA_new(100); ZombieCharBuf *twenty = ZCB_WRAP_STR("20", 2); ZombieCharBuf *forty = ZCB_WRAP_STR("40", 2); ZombieCharBuf *foo = ZCB_WRAP_STR("foo", 3); for (int32_t i = 0; i < 100; i++) { CharBuf *cb = CB_newf("%i32", i); Hash_Store(hash, (Obj*)cb, (Obj*)cb); Hash_Store(dupe, (Obj*)cb, INCREF(cb)); VA_Push(expected, INCREF(cb)); } TEST_TRUE(batch, Hash_Equals(hash, (Obj*)dupe), "Equals"); TEST_INT_EQ(batch, Hash_Get_Capacity(hash), starting_cap, "Initial capacity sufficient (no rebuilds)"); for (int32_t i = 0; i < 100; i++) { Obj *key = VA_Fetch(expected, i); Obj *elem = Hash_Fetch(hash, key); VA_Push(got, (Obj*)INCREF(elem)); } TEST_TRUE(batch, VA_Equals(got, (Obj*)expected), "basic Store and Fetch"); TEST_INT_EQ(batch, Hash_Get_Size(hash), 100, "size incremented properly by Hash_Store"); TEST_TRUE(batch, Hash_Fetch(hash, (Obj*)foo) == NULL, "Fetch against non-existent key returns NULL"); Hash_Store(hash, (Obj*)forty, INCREF(foo)); TEST_TRUE(batch, ZCB_Equals(foo, Hash_Fetch(hash, (Obj*)forty)), "Hash_Store replaces existing value"); TEST_FALSE(batch, Hash_Equals(hash, (Obj*)dupe), "replacement value spoils equals"); TEST_INT_EQ(batch, Hash_Get_Size(hash), 100, "size unaffected after value replaced"); TEST_TRUE(batch, Hash_Delete(hash, (Obj*)forty) == (Obj*)foo, "Delete returns value"); DECREF(foo); TEST_INT_EQ(batch, Hash_Get_Size(hash), 99, "size decremented by successful Delete"); TEST_TRUE(batch, Hash_Delete(hash, (Obj*)forty) == NULL, "Delete returns NULL when key not found"); TEST_INT_EQ(batch, Hash_Get_Size(hash), 99, "size not decremented by unsuccessful Delete"); DECREF(Hash_Delete(dupe, (Obj*)forty)); TEST_TRUE(batch, VA_Equals(got, (Obj*)expected), "Equals after Delete"); Hash_Clear(hash); TEST_TRUE(batch, Hash_Fetch(hash, (Obj*)twenty) == NULL, "Clear"); TEST_TRUE(batch, Hash_Get_Size(hash) == 0, "size is 0 after Clear"); DECREF(hash); DECREF(dupe); DECREF(got); DECREF(expected); }