Skip to content

Commit 98b39d6

Browse files
committed
testing code refactoring.
1 parent 809759c commit 98b39d6

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
35+
build-*/
36+
/test/CMakeLists.txt.user

test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.7)
2+
3+
project(test-ctrl-c)
4+
5+
add_executable(${PROJECT_NAME} "main.cpp" "../src/ctrl-c.h" "../src/ctrl-c.cpp")
6+
target_include_directories(${PROJECT_NAME} PRIVATE "../src")

test/main.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SOFTWARE.
2626
#include <condition_variable>
2727
#include <mutex>
2828

29-
#include "../src/ctrl-c.h"
29+
#include "ctrl-c.h"
3030

3131
int main()
3232
{
@@ -38,22 +38,26 @@ int main()
3838
unsigned int handler_id = CtrlCLibrary::SetCtrlCHandler([&catches, &wait_lock, &wait_var](enum CtrlCLibrary::CtrlSignal event) -> bool {
3939
switch (event) {
4040
case CtrlCLibrary::kCtrlCSignal:
41-
std::cout << "Catch Ctrl+C" << std::endl;
41+
std::cout << "Caught Ctrl+C" << std::endl;
42+
std::lock_guard<std::mutex> locker(wait_lock);
43+
++catches;
44+
wait_var.notify_all();
4245
}
43-
std::lock_guard<std::mutex> locker(wait_lock);
44-
++catches;
45-
wait_var.notify_all();
4646
return true;
4747
});
4848
if (handler_id == CtrlCLibrary::kErrorID) {
4949
std::cerr << "Can't set ctrl+c handler" << std::endl;
50-
return 0;
50+
return 1;
5151
}
52+
5253
std::cout << "Press Ctrl+C " << kMaxCatches << " times" << std::endl;
5354
std::unique_lock<std::mutex> locker(wait_lock);
54-
wait_var.wait(locker, [&catches, kMaxCatches](){
55+
wait_var.wait(locker, [&catches](){
5556
return catches >= kMaxCatches;
5657
});
58+
std::cout << "All Ctrl+C's pressed" << std::endl;
59+
5760
CtrlCLibrary::ResetCtrlCHandler(handler_id);
61+
5862
return 0;
5963
}

0 commit comments

Comments
 (0)