/
SliceTests.cs
175 lines (168 loc) · 8.04 KB
/
SliceTests.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using NppDemo.Utils;
namespace NppDemo.Tests
{
public class SliceTester
{
public static readonly int[] onetofive = new int[] { 1, 2, 3, 4, 5 };
// terminology below:
// + -> 0 <= num <= 4
// - -> -1 >= num >= -5
// big -> num >= 5
// negbig -> num <= -6
// <descriptor>1, <descriptor>2 -> 1 is less than 2 (after doing negative number wraparound)
// e.g., 3:-1 becomes +1:-2 because -1 becomes 4 and 3 < 4
// but -3:4 becomes -2:+1 because -3 becomes 2 and 2 < 4
// and 2:4 becomes +1:+2 because 2 < 4
public static readonly (string slicer, int[] desired)[] str_testcases = new (string slicer, int[] desired)[]
{
(":", onetofive), // :
("2:", new int[]{3, 4, 5}), // +:
("-3:", new int[] { 3, 4, 5 }), // -:
("5:", new int[] { }), // big:
("-24:", onetofive), // negbig:
(":1", new int[]{1}), // :+
(":-3", new int[] { 1, 2 }), // :-
(":85", onetofive), // :big
(":-24", new int[]{}), // :negbig
("1:3", new int[] { 2, 3 }), // +1:+2
("3:1", new int[]{ }), // +2:+1
("3:3", new int[]{}), // +=:+=
("1:-1", new int[] { 2, 3, 4 }), // +1:-2
("4:-3", new int[]{ }), // +2:-1
("3:-2", new int[]{ }), // +=:-=
("4:85", new int[]{5}), // +:big
("3:-8", new int[]{ }), // +:negbig
("-4:4", new int[] { 2, 3, 4 }), // -1:+2
("-3:1", new int[]{}), // -2:+1
("-3:2", new int[]{}), // -=:+=
("-3:-1", new int[]{3, 4}), // -1:-2
("-1:-3", new int[]{}), // -2:-1
("-1:-1", new int[]{}), // -=:-=
("-2:85", new int[] { 4, 5 }), // -:big
("-4:-24", new int[]{}), // -:negbig
("85:1", new int[]{}), // big:+
("85:-3", new int[]{}), // big:-
("85:5", new int[]{}), // big:=
("85:85", new int[] { }), // big:big
("85:-24", new int[]{}), // big:negbig
("-24:85", onetofive), // negbig:big
("-24:-24", new int[] { }), // negbig:negbig
("-24:-5", new int[]{}), // negbig:=
("-24:-4", new int[] { 1 }), // negbig:-
("-24:3", new int[]{1,2,3}), // negbig:+
("1::3", new int[] { 2, 5 }), // +::+
("::-1", new int[] { 5, 4, 3, 2, 1 }), // ::-1
("1:4:2", new int[] { 2, 4 }),
("2::-1", new int[] { 3, 2, 1 }),
("4:1:-2", new int[] { 5, 3 }),
("4:2:-1", new int[] { 5, 4 }),
("-4::2", new int[] { 2, 4 }),
("-3::1", new int[] { 3, 4, 5 }),
("-3::-1", new int[] { 3, 2, 1 }),
("-700:-54:-3", new int[] { }),
("-1:1:-2", new int[] { 5, 3 }),
("-4:4:2", new int[] { 2, 4 }),
("2:-2:2", new int[] { 3 }),
("3::5", new int[] { 4 }),
("13434:4343:8", new int[] { }),
("13434:43:-8", new int[] { }),
("13434:-4:-1", new int[] { 5, 4, 3 }),
("178::-6", new int[] { 5 }),
("9343::-2", new int[] { 5, 3, 1 }),
("86:1:-3", new int[] { 5 }),
("-700:54:9", new int[] { 1 }),
("-24:-5:-1", new int[] { }),
("5:-24:-1", new int[] { 5, 4, 3, 2, 1 }),
("3:-7:-3", new int[] { 4, 1 }),
};
public static bool Test()
{
string input_str = onetofive.ArrayToString();
var testcases = new (int?[] slicer, int[] output)[]
{
(new int?[]{null, null, null}, onetofive),
(new int?[]{null, 1, null}, new int[]{1}),
(new int?[]{null, null, -1}, new int[] {5, 4, 3, 2, 1}),
(new int?[]{1, 3, null}, new int[]{2, 3}),
(new int?[]{1, 4, 2}, new int[]{2, 4}),
(new int?[]{2, null, -1}, new int[]{3, 2, 1}),
(new int?[]{4, 1, -2}, new int[]{5, 3}),
(new int?[]{1, null, 3}, new int[]{2, 5}),
(new int?[]{4, 2, -1}, new int[]{5, 4}),
(new int?[]{-4, -1, null}, new int[]{2,3,4}),
(new int?[]{-4, null, 2}, new int[]{2, 4}),
(new int?[]{null, -3, null}, new int[]{1,2}),
(new int?[]{-3, null, 1}, new int[]{3,4,5}),
(new int?[]{-3, null, -1}, new int[]{3,2,1}),
(new int?[]{-1, 1, -2}, new int[]{5, 3}),
(new int?[]{1, -1, null}, new int[]{2,3,4}),
(new int?[]{-4, 4, null}, new int[]{2,3,4}),
(new int?[]{-4, 4, 2}, new int[]{2, 4}),
(new int?[]{2, -2, 2}, new int[]{3}),
(new int?[]{-4, null, -2}, new int[]{2}),
(new int?[]{2, 1, null}, new int[]{ }),
};
int tests_failed = 0;
int ii = 0;
foreach ((int?[] slicer, int[] desired) in testcases)
{
int[] input = onetofive;
string str_desired = desired.ArrayToString();
int[] output;
ii++;
try
{
output = (int[])input.Slice(slicer);
}
catch (Exception ex)
{
tests_failed++;
Npp.AddLine(String.Format("Test {0} ({1}.Slice({2})) failed:\n" +
"Expected\n{3}\nGot exception\n{6}",
ii + 1, input_str, slicer.ArrayToString(), str_desired, ex));
continue;
}
string str_output = output.ArrayToString();
if (str_output != str_desired)
{
tests_failed++;
Npp.AddLine(String.Format("Test {0} ({1}.Slice({2})) failed:\n" +
"Expected\n{3}\nGot\n{6}",
ii + 1, input_str, slicer.ArrayToString(), str_desired, str_output));
}
}
// test string slicer
foreach ((string slicer, int[] desired) in str_testcases)
{
int[] inp = onetofive;
string str_desired = desired.ArrayToString();
int[] output;
try
{
output = (int[])inp.Slice(slicer);
}
catch (Exception ex)
{
tests_failed++;
Npp.AddLine(String.Format("Test {0} ({1}.Slice(\"{2}\")) failed:\n" +
"Expected\n{3}\nGot exception\n{4}",
ii + 1, input_str, slicer, str_desired, ex));
continue;
}
string str_output = output.ArrayToString();
if (str_output != str_desired)
{
tests_failed++;
Npp.AddLine(String.Format("Test {0} ({1}.Slice(\"{2}\")) failed:\n" +
"Expected\n{3}\nGot\n{4}",
ii + 1, input_str, slicer, str_desired, str_output));
}
ii++;
}
Npp.AddLine($"Failed {tests_failed} tests.");
Npp.AddLine($"Passed {ii - tests_failed} tests.");
return tests_failed > 0;
}
}
}