Skip to content

Commit

Permalink
Port Abseil tests to use actual cwisstable's actual API
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy committed Feb 6, 2022
1 parent 94a0405 commit eb26ed1
Show file tree
Hide file tree
Showing 9 changed files with 1,427 additions and 62 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ http_archive(
# Abseil for our modified Abseil tests.
http_archive(
name = "com_google_absl",
urls = ["https://github.com/abseil/abseil-cpp/archive/98eb410c93ad059f9bba1bf43f5bb916fc92a5ea.zip"],
strip_prefix = "abseil-cpp-98eb410c93ad059f9bba1bf43f5bb916fc92a5ea",
urls = ["https://github.com/abseil/abseil-cpp/archive/36db0e4b695756c948dbb45568b43a62ba5883ec.zip"],
strip_prefix = "abseil-cpp-36db0e4b695756c948dbb45568b43a62ba5883ec",
)
5 changes: 3 additions & 2 deletions cwisstable/declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ CWISS_BEGIN_EXTERN_
static inline HashSet_##_CIter HashSet_##_citer(const HashSet_* self) { \
return (HashSet_##_CIter){CWISS_RawHashSet_citer(&kPolicy_, &self->set_)}; \
} \
static inline const Type_* HashSet_##_CIter_get(const HashSet_##_Iter* it) { \
static inline const Type_* HashSet_##_CIter_get( \
const HashSet_##_CIter* it) { \
return (const Type_*)CWISS_RawIter_get(&kPolicy_, &it->it_); \
} \
static inline const Type_* HashSet_##_CIter_next(HashSet_##_Iter* it) { \
static inline const Type_* HashSet_##_CIter_next(HashSet_##_CIter* it) { \
return (const Type_*)CWISS_RawIter_next(&kPolicy_, &it->it_); \
} \
static inline HashSet_##_CIter HashSet_##_Iter_const(HashSet_##_Iter it) { \
Expand Down
8 changes: 7 additions & 1 deletion cwisstable/internal/extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@
/// `CWISS_EXTRACT_VALUE(k, v)`, which expands to `v`.

#define CWISS_EXTRACT(needle_, default_, ...) \
(CWISS_EXTRACT_RAW(needle_, default_, __VA_ARGS__))

#define CWISS_EXTRACT_RAW(needle_, default_, ...) \
CWISS_EXTRACT00(CWISS_EXTRACT_##needle_, __VA_ARGS__, (needle_, default_))

#define CWISS_EXTRACT_VALUE(key, val) (val)
#define CWISS_EXTRACT_VALUE(key, val) val

// NOTE: Everything below this line is generated by cwisstable/extract.py!

Expand Down Expand Up @@ -96,6 +99,9 @@
#define CWISS_EXTRACT_slot_dtor(key, val) CWISS_EXTRACT_slot_dtorZ##key
#define CWISS_EXTRACT_slot_dtorZslot_dtor \
CWISS_NOTHING, CWISS_NOTHING, CWISS_NOTHING
#define CWISS_EXTRACT_modifiers(key, val) CWISS_EXTRACT_modifiersZ##key
#define CWISS_EXTRACT_modifiersZmodifiers \
CWISS_NOTHING, CWISS_NOTHING, CWISS_NOTHING

#define CWISS_EXTRACT00(needle, kv, ...) \
CWISS_SELECT00(needle kv, CWISS_EXTRACT_VALUE, kv, CWISS_EXTRACT01, \
Expand Down
1 change: 1 addition & 0 deletions cwisstable/internal/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

'slot_size', 'slot_align', 'slot_init',
'slot_transfer', 'slot_get', 'slot_dtor',
'modifiers',
]

def main():
Expand Down
7 changes: 3 additions & 4 deletions cwisstable/internal/raw_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static inline CWISS_RawIter CWISS_RawHashSet_iter_at(const CWISS_Policy* policy,
self->slots_ + index * policy->slot->size,
};
CWISS_RawIter_skip_empty_or_deleted(policy, &iter);
CWISS_AssertIsFull(iter.ctrl_);
CWISS_AssertIsValid(iter.ctrl_);
return iter;
}

Expand Down Expand Up @@ -631,9 +631,8 @@ static inline CWISS_RawHashSet CWISS_RawHashSet_dup(
// `CWISS_RawHashSet_rehash_and_grow_if_necessary()` because we are already
// big enough (since `self` is a priori) and tombstones cannot be created
// during this process.
CWISS_RawIter iter = CWISS_RawHashSet_citer(policy, self);
void* v;
while ((v = CWISS_RawIter_next(policy, &iter))) {
for (CWISS_RawIter iter = CWISS_RawHashSet_citer(policy, self); CWISS_RawIter_get(policy, &iter); CWISS_RawIter_next(policy, &iter)) {
void* v = CWISS_RawIter_get(policy, &iter);
size_t hash = policy->key->hash(v);

CWISS_FindInfo target =
Expand Down
118 changes: 65 additions & 53 deletions cwisstable/policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,59 +266,71 @@ typedef struct {

// ---- PUBLIC API ENDS HERE! ----

#define CWISS_DECLARE_POLICY_(kPolicy_, Type_, Key_, ...) \
CWISS_BEGIN_ \
static inline void kPolicy_##_DefaultCopy(void* dst, const void* src) { \
memcpy(dst, src, sizeof(Type_)); \
} \
static inline size_t kPolicy_##_DefaultHash(const void* val) { \
CWISS_FxHash_State state = 0; \
CWISS_FxHash_Write(&state, val, sizeof(Key_)); \
return state; \
} \
static inline bool kPolicy_##_DefaultEq(const void* a, const void* b) { \
return memcmp(a, b, sizeof(Key_)) == 0; \
} \
static inline void kPolicy_##_DefaultSlotInit(void* slot) {} \
static inline void kPolicy_##_DefaultSlotTransfer(void* dst, void* src) { \
memcpy(dst, src, sizeof(Type_)); \
} \
static inline void* kPolicy_##_DefaultSlotGet(void* slot) { return slot; } \
static inline void kPolicy_##_DefaultSlotDtor(void* slot) { \
if (CWISS_EXTRACT(obj_dtor, NULL, __VA_ARGS__) != NULL) { \
CWISS_EXTRACT(obj_dtor, (void (*)(void*))NULL, __VA_ARGS__)(slot); \
} \
} \
\
static const CWISS_ObjectPolicy kPolicy_##_ObjectPolicy = { \
sizeof(Type_), \
alignof(Type_), \
CWISS_EXTRACT(obj_copy, kPolicy_##_DefaultCopy, __VA_ARGS__), \
CWISS_EXTRACT(obj_dtor, NULL, __VA_ARGS__), \
}; \
static const CWISS_KeyPolicy kPolicy_##_KeyPolicy = { \
CWISS_EXTRACT(key_hash, kPolicy_##_DefaultHash, __VA_ARGS__), \
CWISS_EXTRACT(key_eq, kPolicy_##_DefaultEq, __VA_ARGS__), \
}; \
static const CWISS_AllocPolicy kPolicy_##_AllocPolicy = { \
CWISS_EXTRACT(alloc_alloc, CWISS_DefaultMalloc, __VA_ARGS__), \
CWISS_EXTRACT(alloc_free, CWISS_DefaultFree, __VA_ARGS__), \
}; \
static const CWISS_SlotPolicy kPolicy_##_SlotPolicy = { \
CWISS_EXTRACT(slot_size, sizeof(Type_), __VA_ARGS__), \
CWISS_EXTRACT(slot_align, sizeof(Type_), __VA_ARGS__), \
CWISS_EXTRACT(slot_init, kPolicy_##_DefaultSlotInit, __VA_ARGS__), \
CWISS_EXTRACT(slot_dtor, kPolicy_##_DefaultSlotDtor, __VA_ARGS__), \
CWISS_EXTRACT(slot_transfer, kPolicy_##_DefaultSlotTransfer, \
__VA_ARGS__), \
CWISS_EXTRACT(slot_get, kPolicy_##_DefaultSlotGet, __VA_ARGS__), \
}; \
CWISS_END_ \
static const CWISS_Policy kPolicy_ = { \
&kPolicy_##_ObjectPolicy, \
&kPolicy_##_KeyPolicy, \
&kPolicy_##_AllocPolicy, \
&kPolicy_##_SlotPolicy, \
#define CWISS_DECLARE_POLICY_(kPolicy_, Type_, Key_, ...) \
CWISS_BEGIN_ \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline void kPolicy_##_DefaultCopy(void* dst, const void* src) { \
memcpy(dst, src, sizeof(Type_)); \
} \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline size_t kPolicy_##_DefaultHash(const void* val) { \
CWISS_FxHash_State state = 0; \
CWISS_FxHash_Write(&state, val, sizeof(Key_)); \
return state; \
} \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline bool kPolicy_##_DefaultEq(const void* a, const void* b) { \
return memcmp(a, b, sizeof(Key_)) == 0; \
} \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline void kPolicy_##_DefaultSlotInit(void* slot) {} \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline void kPolicy_##_DefaultSlotTransfer(void* dst, void* src) { \
memcpy(dst, src, sizeof(Type_)); \
} \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline void* kPolicy_##_DefaultSlotGet(void* slot) { return slot; } \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
inline void kPolicy_##_DefaultSlotDtor(void* slot) { \
if (CWISS_EXTRACT(obj_dtor, NULL, __VA_ARGS__) != NULL) { \
CWISS_EXTRACT(obj_dtor, (void (*)(void*))NULL, __VA_ARGS__)(slot); \
} \
} \
\
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
const CWISS_ObjectPolicy kPolicy_##_ObjectPolicy = { \
sizeof(Type_), \
alignof(Type_), \
CWISS_EXTRACT(obj_copy, kPolicy_##_DefaultCopy, __VA_ARGS__), \
CWISS_EXTRACT(obj_dtor, NULL, __VA_ARGS__), \
}; \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
const CWISS_KeyPolicy kPolicy_##_KeyPolicy = { \
CWISS_EXTRACT(key_hash, kPolicy_##_DefaultHash, __VA_ARGS__), \
CWISS_EXTRACT(key_eq, kPolicy_##_DefaultEq, __VA_ARGS__), \
}; \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
const CWISS_AllocPolicy kPolicy_##_AllocPolicy = { \
CWISS_EXTRACT(alloc_alloc, CWISS_DefaultMalloc, __VA_ARGS__), \
CWISS_EXTRACT(alloc_free, CWISS_DefaultFree, __VA_ARGS__), \
}; \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
const CWISS_SlotPolicy kPolicy_##_SlotPolicy = { \
CWISS_EXTRACT(slot_size, sizeof(Type_), __VA_ARGS__), \
CWISS_EXTRACT(slot_align, sizeof(Type_), __VA_ARGS__), \
CWISS_EXTRACT(slot_init, kPolicy_##_DefaultSlotInit, __VA_ARGS__), \
CWISS_EXTRACT(slot_dtor, kPolicy_##_DefaultSlotDtor, __VA_ARGS__), \
CWISS_EXTRACT(slot_transfer, kPolicy_##_DefaultSlotTransfer, \
__VA_ARGS__), \
CWISS_EXTRACT(slot_get, kPolicy_##_DefaultSlotGet, __VA_ARGS__), \
}; \
CWISS_END_ \
CWISS_EXTRACT_RAW(modifiers, static, __VA_ARGS__) \
const CWISS_Policy kPolicy_ = { \
&kPolicy_##_ObjectPolicy, \
&kPolicy_##_KeyPolicy, \
&kPolicy_##_AllocPolicy, \
&kPolicy_##_SlotPolicy, \
}

#define CWISS_DECLARE_NODE_FUNCTIONS_(kPolicy_, Type_, ...) \
Expand Down
23 changes: 23 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ cc_library(
linkopts = CWISS_DEFAULT_LINKOPTS,
)

cc_library(
name = "test_helpers",
hdrs = ["test_helpers.h"],
deps = ["//:unified"],
copts = CWISS_DEFAULT_COPTS + CWISS_CXX_VERSION,
linkopts = CWISS_DEFAULT_LINKOPTS,
)

cc_test(
name = "raw_hash_set_test",
srcs = ["absl_raw_hash_set_test.cc"],
Expand All @@ -47,6 +55,21 @@ cc_test(
linkopts = CWISS_DEFAULT_LINKOPTS,
)

cc_test(
name = "cwisstable_test",
srcs = ["cwisstable_test.cc"],
deps = [
":test_helpers",
"//:unified",
"//:debug",

"@com_google_absl//absl/cleanup",
"@com_google_googletest//:gtest_main",
],
copts = CWISS_TEST_COPTS + CWISS_CXX_VERSION,
linkopts = CWISS_DEFAULT_LINKOPTS,
)

cc_binary(
name = "raw_hash_set_benchmark",
testonly = 1,
Expand Down
Loading

0 comments on commit eb26ed1

Please sign in to comment.