Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Aug 3, 2023
1 parent 2dbf302 commit fd606f9
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,99 @@ namespace xtd::tests {
assert::is_true(thread_ran, csf_);
assert::are_equal(wait_handle::invalid_handle, s.handle(), csf_);
}

void test_method_(open_existing_with_same_name) {
if (environment::os_version().is_windows() && !environment::is_64_bit_process()) assert::ignore();
auto created_new = false;
auto s = semaphore {"xtd_semaphore_test", created_new};
assert::are_not_equal(wait_handle::invalid_handle, s.handle(), csf_);
auto thread_ran = false;
auto thread = std::thread {[&] {
auto s2 = semaphore::open_existing("xtd_semaphore_test");
assert::are_not_equal(wait_handle::invalid_handle, s2.handle(), csf_);
thread_ran = true;
}};
thread.join();
assert::is_true(thread_ran, csf_);
}

void test_method_(open_existing_with_different_name) {
if (environment::os_version().is_windows() && !environment::is_64_bit_process()) assert::ignore();
auto created_new = false;
auto s = semaphore {"xtd_semaphore_test", created_new};
assert::are_not_equal(wait_handle::invalid_handle, s.handle(), csf_);
auto thread_ran = false;
auto thread = std::thread {[&] {
assert::throws<io::io_exception>([] {auto s2 = semaphore::open_existing("xtd_semaphore_test_2");}, csf_);
thread_ran = true;
}};
thread.join();
assert::is_true(thread_ran, csf_);
}

void test_method_(open_existing_with_empty_name) {
if (environment::os_version().is_windows() && !environment::is_64_bit_process()) assert::ignore();
auto created_new = false;
auto s = semaphore {"xtd_semaphore_test", created_new};
assert::are_not_equal(wait_handle::invalid_handle, s.handle(), csf_);
auto thread_ran = false;
auto thread = std::thread {[&] {
assert::throws<argument_exception>([] {auto s2 = semaphore::open_existing("");}, csf_);
thread_ran = true;
}};
thread.join();
assert::is_true(thread_ran, csf_);
}

void test_method_(try_open_existing_with_same_name) {
if (environment::os_version().is_windows() && !environment::is_64_bit_process()) assert::ignore();
auto created_new = false;
auto s = semaphore {"xtd_semaphore_test", created_new};
assert::are_not_equal(wait_handle::invalid_handle, s.handle(), csf_);
auto thread_ran = false;
auto thread = std::thread {[&] {
auto s2 = semaphore {};
auto result = semaphore::try_open_existing("xtd_semaphore_test", s2);
assert::is_true(result, csf_);
assert::are_not_equal(wait_handle::invalid_handle, s2.handle(), csf_);
thread_ran = true;
}};
thread.join();
assert::is_true(thread_ran, csf_);
}

void test_method_(try_open_existing_with_different_name) {
if (environment::os_version().is_windows() && !environment::is_64_bit_process()) assert::ignore();
auto created_new = false;
auto s = semaphore {"xtd_semaphore_test", created_new};
assert::are_not_equal(wait_handle::invalid_handle, s.handle(), csf_);
auto thread_ran = false;
auto thread = std::thread {[&] {
auto s2 = semaphore {};
auto result = semaphore::try_open_existing("xtd_semaphore_test_2", s2);
assert::is_false(result, csf_);
assert::are_equal(wait_handle::invalid_handle, s2.handle(), csf_);
thread_ran = true;
}};
thread.join();
assert::is_true(thread_ran, csf_);
}

void test_method_(try_open_existing_with_empty_name) {
if (environment::os_version().is_windows() && !environment::is_64_bit_process()) assert::ignore();
auto created_new = false;
auto s = semaphore {"xtd_semaphore_test", created_new};
assert::are_not_equal(wait_handle::invalid_handle, s.handle(), csf_);
auto thread_ran = false;
auto thread = std::thread {[&] {
auto s2 = semaphore {};
auto result = semaphore::try_open_existing("", s2);
assert::is_false(result, csf_);
assert::are_equal(wait_handle::invalid_handle, s2.handle(), csf_);
thread_ran = true;
}};
thread.join();
assert::is_true(thread_ran, csf_);
}
};
}

0 comments on commit fd606f9

Please sign in to comment.