1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/sync/atomic2/atomic_test.v

96 lines
2.0 KiB
V
Raw Normal View History

2020-06-27 00:31:03 +03:00
import atomic2
2020-06-26 21:04:17 +03:00
import sync
struct Counter {
mut:
counter u64 = 0
}
// without proper syncronization this would fail
fn test_count_100_milion_should_result_100_million() {
mut wg := sync.new_waitgroup()
mut counter := &Counter{}
wg.add(10)
for i := 0; i < 10; i++ {
go count_ten_million(mut counter, mut wg)
}
wg.wait()
assert counter.counter == 10000000
}
// This test just to make sure that we have an anti-test to prove it works
2020-06-27 00:31:03 +03:00
// fn test_count_100_milion_should_fail_100_million_without_sync() {
// mut wg := sync.new_waitgroup()
// mut counter := &Counter{}
// wg.add(10)
// for i := 0; i < 10; i++ {
// go count_ten_million_without_sync(mut counter, mut wg)
// }
// wg.wait()
// assert counter.counter != 10000000
// }
2020-06-26 21:04:17 +03:00
fn test_count_plus_one_u64() {
mut c := u64(0)
2020-06-27 00:31:03 +03:00
atomic2.add_u64(&c, 1)
2020-06-26 21:04:17 +03:00
assert c == 1
}
fn test_count_plus_one_i64() {
mut c := i64(0)
2020-06-27 00:31:03 +03:00
atomic2.add_i64(&c, 1)
2020-06-26 21:04:17 +03:00
assert c == 1
}
fn test_count_plus_greater_than_one_u64() {
mut c := u64(0)
2020-06-27 00:31:03 +03:00
atomic2.add_u64(&c, 10)
2020-06-26 21:04:17 +03:00
assert c == 10
}
fn test_count_plus_greater_than_one_i64() {
mut c := i64(0)
2020-06-27 00:31:03 +03:00
atomic2.add_i64(&c, 10)
2020-06-26 21:04:17 +03:00
assert c == 10
}
fn test_count_minus_one_u64() {
mut c := u64(1)
2020-06-27 00:31:03 +03:00
atomic2.sub_u64(&c, 1)
2020-06-26 21:04:17 +03:00
assert c == 0
}
fn test_count_minus_one_i64() {
mut c := i64(0)
2020-06-27 00:31:03 +03:00
atomic2.sub_i64(&c, 1)
2020-06-26 21:04:17 +03:00
assert c == -1
}
fn test_count_minus_greater_than_one_u64() {
mut c := u64(10)
2020-06-27 00:31:03 +03:00
atomic2.sub_u64(&c, 10)
2020-06-26 21:04:17 +03:00
assert c == 0
}
fn test_count_minus_greater_than_one_i64() {
mut c := i64(10)
2020-06-27 00:31:03 +03:00
atomic2.sub_i64(&c, 20)
2020-06-26 21:04:17 +03:00
assert c == -10
}
// count_ten_million counts the common counter 10 million times in thread-safe way
fn count_ten_million(mut counter Counter, mut group sync.WaitGroup) {
for i := 0; i < 1000000; i++ {
2020-06-27 00:31:03 +03:00
atomic2.add_u64(&counter.counter, 1)
2020-06-26 21:04:17 +03:00
}
group.done()
}
2020-06-27 00:31:03 +03:00
// // count_ten_million_without_sync counts the common counter 10 million times in none thread-safe way
// fn count_ten_million_without_sync(mut counter Counter, mut group sync.WaitGroup) {
// for i := 0; i < 1000000; i++ {
// counter.counter++
// }
// group.done()
// }