#include <stdio.h>
#include <assert.h>
#include "../runtime/methodcache.h"
static int tests_passed = 0;
#define TEST(name) static void test_##name(void)
#define RUN_TEST(name) do { \
printf("Running %s... ", #name); \
test_##name(); \
printf("PASSED\n"); \
tests_passed++; \
} while(0)
TEST(create_destroy) {
MethodCache_t* cache = rava_methodcache_create();
assert(cache != NULL);
rava_methodcache_destroy(cache);
}
TEST(insert_lookup) {
MethodCache_t* cache = rava_methodcache_create();
assert(cache != NULL);
int dummy_method = 42;
RavaMethod_t* method = (RavaMethod_t*)&dummy_method;
rava_methodcache_insert(cache, "TestClass", "testMethod", method);
RavaMethod_t* found = rava_methodcache_lookup(cache, "TestClass", "testMethod");
assert(found == method);
rava_methodcache_destroy(cache);
}
TEST(lookup_miss) {
MethodCache_t* cache = rava_methodcache_create();
assert(cache != NULL);
RavaMethod_t* found = rava_methodcache_lookup(cache, "NonExistent", "method");
assert(found == NULL);
rava_methodcache_destroy(cache);
}
TEST(multiple_methods) {
MethodCache_t* cache = rava_methodcache_create();
assert(cache != NULL);
int m1 = 1, m2 = 2;
RavaMethod_t* method1 = (RavaMethod_t*)&m1;
RavaMethod_t* method2 = (RavaMethod_t*)&m2;
rava_methodcache_insert(cache, "TestClass", "methodA", method1);
RavaMethod_t* found1 = rava_methodcache_lookup(cache, "TestClass", "methodA");
assert(found1 == method1);
rava_methodcache_insert(cache, "OtherClass", "methodB", method2);
RavaMethod_t* found2 = rava_methodcache_lookup(cache, "OtherClass", "methodB");
assert(found2 == method2);
rava_methodcache_destroy(cache);
}
TEST(clear) {
MethodCache_t* cache = rava_methodcache_create();
assert(cache != NULL);
int dummy = 42;
RavaMethod_t* method = (RavaMethod_t*)&dummy;
rava_methodcache_insert(cache, "TestClass", "testMethod", method);
assert(rava_methodcache_lookup(cache, "TestClass", "testMethod") == method);
rava_methodcache_clear(cache);
assert(rava_methodcache_lookup(cache, "TestClass", "testMethod") == NULL);
rava_methodcache_destroy(cache);
}
TEST(null_params) {
MethodCache_t* cache = rava_methodcache_create();
rava_methodcache_insert(NULL, "Class", "method", NULL);
rava_methodcache_insert(cache, NULL, "method", NULL);
rava_methodcache_insert(cache, "Class", NULL, NULL);
assert(rava_methodcache_lookup(NULL, "Class", "method") == NULL);
assert(rava_methodcache_lookup(cache, NULL, "method") == NULL);
assert(rava_methodcache_lookup(cache, "Class", NULL) == NULL);
rava_methodcache_destroy(cache);
}
TEST(hash_function) {
uint32_t h1 = rava_methodcache_hash("hello");
uint32_t h2 = rava_methodcache_hash("hello");
uint32_t h3 = rava_methodcache_hash("world");
assert(h1 == h2);
assert(h1 != h3);
assert(h1 != 0);
assert(h3 != 0);
}
int main(void) {
printf("=== Method Cache Unit Tests ===\n\n");
RUN_TEST(create_destroy);
RUN_TEST(insert_lookup);
RUN_TEST(lookup_miss);
RUN_TEST(multiple_methods);
RUN_TEST(clear);
RUN_TEST(null_params);
RUN_TEST(hash_function);
printf("\n=== Results: %d tests passed ===\n", tests_passed);
return 0;
}