comfortable-swipe/tests/test_regex.cpp
Rico Tiongson e3453c1bbb
Perform microoptimizations (#40)
* Remove unused device and stamp from swipe_impl

* Use constants for regex patterns

* Compile regex outside of buffer function to avoid runtime hiccup

* Optimize reading of config file

* Revert "Optimize reading of config file"

This reverts commit 88b85d3941.

* Improve tokenizing of config file

* Make sentence string static

* Improve README; change default threshold to 20.0

* Add a flag for gesture begin to ignore unneeded update/end

* Pre-compute for square of threshold and scale to lessen computation

* Compare fingers string with only one digit

* Use fgets_unlocked for faster input stream reading

* Don't buffer error stream

* Set some variables to static

* Catch dash symbol before event, remove trimming

* Use const char* for conf_filename

* Fix error in printing help

* Add some test scripts
2019-02-07 17:51:47 +08:00

57 lines
2.3 KiB
C++

#include <iostream>
#include <cassert>
#include <regex>
#include "../src/comfortable-swipe.hpp"
namespace test {
void gesture_begin_should_match_regex();
void gesture_update_should_match_regex();
void gesture_end_should_match_regex();
}
int main() {
std::cout << "(1) Testing gesture_begin_should_match_regex()" << std::endl;
test::gesture_begin_should_match_regex();
std::cout << "(2) Testing gesture_begin_should_match_regex()" << std::endl;
test::gesture_update_should_match_regex();
std::cout << "(3) Testing gesture_begin_should_match_regex()" << std::endl;
test::gesture_end_should_match_regex();
std::cout << "ALL TEST PASSED" << std::endl;
}
namespace test {
void gesture_begin_test(const char* data, const char* expected_fingers) {
std::cout << " testing against \"" << data << "\"...";
std::cmatch matches;
auto result = std::regex_match(data, matches, service::gesture_begin);
assert(result != 0);
assert((string) matches[1] == expected_fingers);
std::cout << "PASSED" << std::endl;
}
void gesture_begin_should_match_regex() {
test::gesture_begin_test(" event15 GESTURE_SWIPE_BEGIN +34.33s 3\n", "3");
test::gesture_begin_test("-event4 GESTURE_SWIPE_BEGIN +3.12s 4\n", "4");
test::gesture_begin_test("-event7 GESTURE_SWIPE_BEGIN +4.72s 3\n", "3");
test::gesture_begin_test(" event9 GESTURE_SWIPE_BEGIN +45.80s 4\n", "4");
}
void gesture_update_should_match_regex() {
const char* data = " event15 GESTURE_SWIPE_UPDATE +34.70s 3 -0.12/ 4.99 (-0.33/13.50 unaccelerated)\n";
std::cmatch matches;
auto result = std::regex_match(data, matches, service::gesture_update);
assert(result != 0);
assert((string) matches[1] == "3");
assert((string) matches[2] == "-0.12");
assert((string) matches[3] == "4.99");
assert((string) matches[4] == "-0.33");
assert((string) matches[5] == "13.50");
}
void gesture_end_should_match_regex() {
const char* data = " event15 GESTURE_SWIPE_END +35.03s 3\n";
std::cmatch matches;
auto result = std::regex_match(data, matches, service::gesture_end);
assert(result != 0);
assert((string) matches[1] == "3");
}
}