File: test_typing_line.cc

package info (click to toggle)
logserver 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: cpp: 10,815; makefile: 3
file content (128 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <random>

#include "test.h"

#include "typing_line.h"

TEST_CASE("insertion") {
	TypingLine tl;
	tl.process('h');
	tl.process('l');
	tl.process('l');
	tl.process(KEY_LEFT);
	tl.process(KEY_LEFT);
	tl.process('e');
	tl.process(KEY_END);
	tl.process('o');
	tl.process('\n');
	CHECK(tl.result());
	CHECK(tl.typed() == "hello");
}

TEST_CASE("insertion home") {
	TypingLine tl;
	tl.process('l');
	tl.process('l');
	tl.process(KEY_HOME);
	tl.process('e');
	tl.process(KEY_LEFT);
	tl.process('h');
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process('o');
	tl.process('\n');
	CHECK(tl.result());
	CHECK(tl.typed() == "hello");
}

TEST_CASE("insertion empty") {
	TypingLine tl;
	tl.process(KEY_HOME);
	tl.process(KEY_LEFT);
	tl.process(KEY_LEFT);
	tl.process(KEY_LEFT);
	tl.process('h');
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process(KEY_RIGHT);
	tl.process('i');
	tl.process('\n');
	CHECK(tl.result());
	CHECK(tl.typed() == "hi");
}

TEST_CASE("completion") {
	TypingLine tl;
	tl.set_type("hello");
	tl.process('\n');
	tl.set_type("world");
	tl.process('\n');

	TypingLine other_tl;
	other_tl.process('h');
	other_tl.process('\t');
	CHECK(other_tl.typed() == "hello");
	other_tl.process(KEY_HOME);
	other_tl.process('w');
	other_tl.process('\t');
	CHECK(other_tl.typed() == "world");
}

TEST_CASE("completion order") {
	TypingLine tl;
	tl.set_type("hello");
	tl.process('\n');
	tl.set_type("heath");
	tl.process('\n');

	TypingLine other_tl;
	other_tl.process('h');
	other_tl.process('e');
	other_tl.process('\t');
	CHECK(other_tl.typed() == "heath");
	other_tl.process('\b');
	other_tl.process('\b');
	other_tl.process('\b');
	other_tl.process('\b');
	other_tl.process('\b');
	other_tl.process('\b');
	other_tl.process('\b');
	other_tl.process('w');
	other_tl.process('w');
	other_tl.process('w');
	other_tl.process('\t');
	CHECK(other_tl.typed() == "www");
}

// must be after completion test because it add random words to dictionary
TEST_CASE("random typing") {
	mt19937 rng(random_device{}());
	uniform_int_distribution<int> action(0, 1);
	uniform_int_distribution<unsigned char> chardist('a', 'z');
	for (int i = 0; i < 100; ++i) {
		TypingLine tl;
		CHECK(!tl.has_result());
		string s;
		for (int j = 0; j < 1000; ++j) {
			if (action(rng)) {
				char c = chardist(rng);
				s.push_back(c);
				tl.process(c);
			} else {
				if (!s.empty()) s.pop_back();
				tl.process('\b');
			}
			CHECK(!tl.has_result());
			CHECK(tl.typed() == s);
		}
		tl.process('\n');
		CHECK(tl.result());
	}
}