libcx/test/test.cpp

95 lines
2.2 KiB
C++

#include <cx/io/print.hpp>
#include <cx/std.hpp>
#include <cx/format/obj_fmt_helper.hpp>
#include <cx/sync/mutex.hpp>
#include <cx/sync/spinlock.hpp>
#include <stddef.h>
#include <pthread.h>
#include <unistd.h>
#include <cx/logger.hpp>
struct Foo {
int bar = 1;
float baz = 2.0;
// IMPL: Format
String fmt_to_str() {
auto h = ObjFmtHelper("Foo");
h.add_field("bar", this->bar);
h.add_field("baz", this->baz);
return h.finish();
}
};
Vec<int> test_vec() {
Vec<int> v = Vec<int>();
v.push(1);
v.push(2);
v.push(3);
v.push(4);
return v;
}
void test_fmt(Vec<int> v) {
Foo f = Foo();
println("Hello!!!! {}, {}, {}, {}", "meow", 420.0, 49, v);
println("> {}", f);
}
extern "C" void* test_mutex_thread(void* arg) {
Mutex<int>* m = (Mutex<int>*)arg;
auto val = m->lock();
println("mutex: changing {} to 69", *val);
*val = 69;
println("mutex: 5 second eep!! (eepy boyo)");
sleep(5);
return NULL;
}
void test_mutex() {
Mutex<int>* m = new Mutex<int>(0);
pthread_t thread;
println("mutex: Creating thread for mutex");
if (pthread_create(&thread, NULL, test_mutex_thread, (void*)m) != 0) {
println("Uh oh (mutex)");
}
pthread_join(thread, NULL);
auto val = m->lock();
println("mutex: Value was {}", *val);
}
extern "C" void* test_spinlock_thread(void* arg) {
SpinLock<int>* sl = (SpinLock<int>*)arg;
println("spinlock: changing {} to 5", *sl->lock());
*sl->lock() = 5;
println("spinlock: 5 second eep!! (eepy girlie)");
sleep(5);
return NULL;
}
void test_spinlock() {
SpinLock<int>* sl = new SpinLock<int>(0);
pthread_t thread;
println("spinlock: Creating thread for spinlock");
if (pthread_create(&thread, NULL, test_spinlock_thread, (void*)sl) != 0) {
println("Uh oh (spinlock)");
}
pthread_join(thread, NULL);
println("spinlock: Value was {}", *sl->lock());
}
/// https://github.com/yosh-matsuda/field-reflection
int main(int argc, char* argv[]) {
// Vec<int> v = test_vec();
// test_fmt(v);
// test_spinlock();
// test_mutex();
log::debug("hemlo!!");
log::info("hemlo!!");
log::warn("hemlo!!");
log::error("hemlo!!");
return 0;
}