mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
- `write` of qoi would leak memory. - Issue when having an empty `Path` or just "." - `set_env` would leak memory.
90 lines
1.5 KiB
Plaintext
90 lines
1.5 KiB
Plaintext
import std::thread;
|
|
import std::io;
|
|
|
|
int a;
|
|
|
|
fn void testrun() @test => mem::@scoped(&allocator::LIBC_ALLOCATOR)
|
|
{
|
|
Thread t;
|
|
a = 0;
|
|
t.create(fn int(void* arg) { a++; return 0; }, null)!!;
|
|
assert(t.join()!! == 0);
|
|
assert(a == 1);
|
|
|
|
t.create(fn int(void* arg) { return 10; }, null)!!;
|
|
assert(t.join()!! == 10);
|
|
}
|
|
|
|
|
|
Mutex m_global;
|
|
|
|
fn void testrun_mutex() @test => mem::@scoped(&allocator::LIBC_ALLOCATOR)
|
|
{
|
|
Thread[20] ts;
|
|
a = 0;
|
|
m_global.init()!!;
|
|
foreach (&t : ts)
|
|
{
|
|
t.create(fn int(void* arg) {
|
|
m_global.lock()!!;
|
|
defer m_global.unlock()!!;
|
|
a += 10;
|
|
thread::sleep_ms(5);
|
|
a *= 10;
|
|
thread::sleep_ms(5);
|
|
a /= 10;
|
|
thread::sleep_ms(5);
|
|
a -= 10;
|
|
thread::sleep_ms(5);
|
|
a++;
|
|
return 0;
|
|
}, null)!!;
|
|
}
|
|
foreach (&t : ts)
|
|
{
|
|
assert(t.join()!! == 0);
|
|
}
|
|
assert(a == ts.len);
|
|
m_global.destroy()!!;
|
|
}
|
|
|
|
fn void testrun_mutex_try() @test => mem::@scoped(&allocator::LIBC_ALLOCATOR)
|
|
{
|
|
Mutex m;
|
|
m.init()!!;
|
|
m.lock()!!;
|
|
assert(m.try_lock() == false);
|
|
m.unlock()!!;
|
|
assert(m.try_lock() == true);
|
|
m.unlock()!!;
|
|
}
|
|
|
|
fn void testrun_mutex_timeout() @test => mem::@scoped(&allocator::LIBC_ALLOCATOR)
|
|
{
|
|
TimedMutex m;
|
|
m.init()!!;
|
|
m.lock()!!;
|
|
if (try m.lock_timeout(20))
|
|
{
|
|
unreachable("lock_timeout should fail");
|
|
}
|
|
m.unlock()!!;
|
|
m.lock_timeout(20)!!;
|
|
m.unlock()!!;
|
|
}
|
|
|
|
int x_once = 100;
|
|
|
|
fn void call_once()
|
|
{
|
|
x_once += 100;
|
|
}
|
|
|
|
fn void testrun_once() @test => mem::@scoped(&allocator::LIBC_ALLOCATOR)
|
|
{
|
|
OnceFlag once;
|
|
once.call(&call_once);
|
|
assert(x_once == 200);
|
|
once.call(&call_once);
|
|
assert(x_once == 200);
|
|
} |