Skip to content

Commit 199b3f0

Browse files
test: make single index based access tests parameterized
This way, we can test the same behavior for vectors and lists (with named and unnamed arguments).
1 parent 9d4e953 commit 199b3f0

File tree

2 files changed

+89
-49
lines changed

2 files changed

+89
-49
lines changed

test/functionality/dataflow/pointer-analysis/list-single-index-based-access.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe.sequential('List Single Index Based Access', withShell(shell => {
3535
resolveIdsAsCriterion: true,
3636
}
3737
);
38-
38+
3939
assertDataflow(
4040
label('When single nested index is accessed, then access reads index', capabilities),
4141
shell,
@@ -51,7 +51,7 @@ describe.sequential('List Single Index Based Access', withShell(shell => {
5151
}
5252
);
5353
});
54-
54+
5555
describe('Access with assignment', () => {
5656
assertDataflow(
5757
label('When single index is assigned, then access reads index in assignment and definition', capabilities),
@@ -69,7 +69,7 @@ describe.sequential('List Single Index Based Access', withShell(shell => {
6969
resolveIdsAsCriterion: true,
7070
}
7171
);
72-
72+
7373
assertDataflow(
7474
label('When several indices are assigned, then access reads only correct index in assignment and definition', capabilities),
7575
shell,
@@ -110,7 +110,7 @@ describe.sequential('List Single Index Based Access', withShell(shell => {
110110
resolveIdsAsCriterion: true,
111111
}
112112
);
113-
113+
114114
assertDataflow(
115115
label('When single nested index is accessed, then access reads index', capabilities),
116116
shell,
@@ -126,7 +126,7 @@ describe.sequential('List Single Index Based Access', withShell(shell => {
126126
}
127127
);
128128
});
129-
129+
130130
describe('Access with assignment', () => {
131131
assertDataflow(
132132
label('When single index is assigned, then access reads index in assignment and definition', capabilities),
@@ -144,7 +144,7 @@ describe.sequential('List Single Index Based Access', withShell(shell => {
144144
resolveIdsAsCriterion: true,
145145
}
146146
);
147-
147+
148148
assertDataflow(
149149
label('When several indices are assigned, then access reads only correct index in assignment and definition', capabilities),
150150
shell,

test/functionality/slicing/pointer-analysis/vector-single-index-based-access.test.ts test/functionality/slicing/pointer-analysis/container-single-index-based-access.test.ts

+83-43
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,89 @@ import { assertSliced, withShell } from '../../_helper/shell';
33
import { useConfigForTest } from '../../_helper/config';
44
import { label } from '../../_helper/label';
55

6-
describe.sequential('Vector Single Index Based Access', withShell(shell => {
7-
describe.each([{ type: '[[' }, { type: '[' }])('Access using $type', ({ type }) => {
6+
describe.sequential('Container Single Index Based Access', withShell(shell => {
7+
describe.each(
8+
[
9+
{ container: 'c', type: '[[', hasNamedArguments: false },
10+
{ container: 'c', type: '[', hasNamedArguments: false },
11+
{ container: 'list', type: '[[', hasNamedArguments: false },
12+
{ container: 'list', type: '[', hasNamedArguments: false },
13+
{ container: 'list', type: '[[', hasNamedArguments: true },
14+
{ container: 'list', type: '[', hasNamedArguments: true },
15+
]
16+
)('Access for container $container using $type and hasNamedArguments $hasNamedArguments', ({ container, type, hasNamedArguments }) => {
817
const basicCapabilities = [
918
'name-normal',
1019
'function-calls',
11-
'unnamed-arguments',
20+
hasNamedArguments ? 'named-arguments' : 'unnamed-arguments',
1221
'subsetting',
1322
type === '[[' ? 'double-bracket-access' : 'single-bracket-access'
1423
] as const;
1524
useConfigForTest({ solver: { pointerTracking: true } });
1625

26+
/**
27+
* Creates access string
28+
*
29+
* Example for name='numbers', index=1 and type='[[':
30+
* ```r
31+
* numbers[[1]]
32+
* ```
33+
*/
1734
function acc(name: string, index: number) {
1835
const closingBracket = type === '[[' ? ']]' : ']';
1936
return `${name}${type}${index}${closingBracket}`;
2037
}
21-
38+
39+
/**
40+
* Creates definition string
41+
*
42+
* Example for values=['1', '2', '3', '4'], container='list' and hasNamedArguments=true:
43+
* ```r
44+
* list(arg1 = 1, arg2 = 2, arg3 = 3, arg4 = 4)
45+
* ```
46+
*/
47+
function def(...values: string[]) {
48+
const parameterList = values.map((value, i) => {
49+
if(hasNamedArguments) {
50+
return `arg${i + 1} = ${value}`;
51+
} else {
52+
return value;
53+
}
54+
}).join(', ');
55+
return `${container}(${parameterList})`;
56+
}
57+
2258
describe('Simple access', () => {
23-
assertSliced(label('Vector with single argument', basicCapabilities), shell,
24-
`numbers <- c(2)\nprint(${acc('numbers', 1)})`,
59+
assertSliced(label('Container with single argument', basicCapabilities), shell,
60+
`numbers <- ${def('2')}
61+
print(${acc('numbers', 1)})`,
2562
['2@print'],
26-
`numbers <- c(2)\nprint(${acc('numbers', 1)})`,
63+
`numbers <- ${def('2')}
64+
print(${acc('numbers', 1)})`,
2765
);
2866

2967
/* we reconstruct everything as every other modification could mess with the correctness of the result */
30-
assertSliced(label('vector with several arguments', basicCapabilities), shell,
31-
`numbers <- c(1, 2, 3, 4)\nprint(${acc('numbers', 1)})`,
68+
assertSliced(label('Container with several arguments', basicCapabilities), shell,
69+
`numbers <- ${def('1', '2', '3', '4')}
70+
print(${acc('numbers', 1)})`,
3271
['2@print'],
33-
`numbers <- c(1, 2, 3, 4)\nprint(${acc('numbers', 1)})`,
72+
`numbers <- ${def('1', '2', '3', '4')}
73+
print(${acc('numbers', 1)})`,
3474
);
3575
});
3676

37-
describe('Whole vector access', () => {
77+
describe('Whole container access', () => {
3878
assertSliced(
39-
label('When each argument of a vector is redefined, then original vector is still in slice', basicCapabilities),
79+
label('When each argument of a container is redefined, then original container is still in slice', basicCapabilities),
4080
shell,
41-
`numbers <- c(1, 2, 3, 4)
81+
`numbers <- ${def('1', '2', '3', '4')}
4282
${acc('numbers', 1)} <- 4
4383
${acc('numbers', 2)} <- 3
4484
${acc('numbers', 3)} <- 2
4585
${acc('numbers', 4)} <- 1
4686
print(numbers)`,
4787
['6@print'],
48-
`numbers <- c(1, 2, 3, 4)
88+
`numbers <- ${def('1', '2', '3', '4')}
4989
${acc('numbers', 1)} <- 4
5090
${acc('numbers', 2)} <- 3
5191
${acc('numbers', 3)} <- 2
@@ -54,16 +94,16 @@ print(numbers)`
5494
);
5595

5696
assertSliced(
57-
label('When arguments are added to an empty vector, then original vector is in slice', basicCapabilities),
97+
label('When arguments are added to an empty container, then original container is in slice', basicCapabilities),
5898
shell,
59-
`x <- c()
99+
`x <- ${def()}
60100
${acc('x', 1)} <- 1
61101
${acc('x', 2)} <- 2
62102
${acc('x', 3)} <- 3
63103
${acc('x', 4)} <- 4
64104
print(x)`,
65105
['6@print'],
66-
`x <- c()
106+
`x <- ${def()}
67107
${acc('x', 1)} <- 1
68108
${acc('x', 2)} <- 2
69109
${acc('x', 3)} <- 3
@@ -72,17 +112,17 @@ print(x)`
72112
);
73113

74114
assertSliced(
75-
label('When whole vector is redefined, then every vector assignment before is not in slice', basicCapabilities),
115+
label('When whole container is redefined, then every container assignment before is not in slice', basicCapabilities),
76116
shell,
77-
`numbers <- c(1, 2)
117+
`numbers <- ${def('1', '2')}
78118
${acc('numbers', 1)} <- 2
79119
${acc('numbers', 2)} <- 1
80-
numbers <- c(3, 4)
120+
numbers <- ${def('3', '4')}
81121
${acc('numbers', 1)} <- 4
82122
${acc('numbers', 2)} <- 3
83123
print(numbers)`,
84124
['7@print'],
85-
`numbers <- c(3, 4)
125+
`numbers <- ${def('3', '4')}
86126
${acc('numbers', 1)} <- 4
87127
${acc('numbers', 2)} <- 3
88128
print(numbers)`
@@ -91,26 +131,26 @@ print(numbers)`
91131

92132
describe('Access with other accesses', () => {
93133
assertSliced(
94-
label('With other vector', basicCapabilities),
134+
label('With other container', basicCapabilities),
95135
shell,
96-
`numbers <- c(1, 2)
97-
other_numbers <- c(3, 4)
136+
`numbers <- ${def('1', '2')}
137+
other_numbers <- ${def('3', '4')}
98138
a <- ${acc('other_numbers', 1)}
99139
print(${acc('numbers', 1)})`,
100140
['4@print'],
101-
`numbers <- c(1, 2)
141+
`numbers <- ${def('1', '2')}
102142
print(${acc('numbers', 1)})`,
103143
);
104144

105145
assertSliced(
106146
label('With other accesses', basicCapabilities),
107147
shell,
108-
`numbers <- c(1, 2)
148+
`numbers <- ${def('1', '2')}
109149
a <- ${acc('numbers', 1)}
110150
b <- ${acc('numbers', 2)}
111151
print(${acc('numbers', 1)})`,
112152
['4@print'],
113-
`numbers <- c(1, 2)
153+
`numbers <- ${def('1', '2')}
114154
print(${acc('numbers', 1)})`,
115155
);
116156
});
@@ -119,72 +159,72 @@ print(${acc('numbers', 1)})`,
119159
assertSliced(
120160
label('When there is more than one assignment to the same index, then the last assignment is in the slice', basicCapabilities),
121161
shell,
122-
`numbers <- c(1, 2)
162+
`numbers <- ${def('1', '2')}
123163
${acc('numbers', 1)} <- 3
124164
${acc('numbers', 1)} <- 4
125165
${acc('numbers', 1)} <- 5
126166
print(${acc('numbers', 1)})`,
127167
['5@print'],
128-
`numbers <- c(1, 2)
168+
`numbers <- ${def('1', '2')}
129169
${acc('numbers', 1)} <- 5
130170
print(${acc('numbers', 1)})`,
131171
);
132172

133173
assertSliced(
134174
label('When there are assignments to the other indices, then they are not in the slice', basicCapabilities),
135175
shell,
136-
`numbers <- c(1, 2, 3)
176+
`numbers <- ${def('1', '2', '3')}
137177
${acc('numbers', 1)} <- 4
138178
${acc('numbers', 2)} <- 5
139179
${acc('numbers', 3)} <- 6
140180
print(${acc('numbers', 1)})`,
141181
['5@print'],
142-
`numbers <- c(1, 2, 3)
182+
`numbers <- ${def('1', '2', '3')}
143183
${acc('numbers', 1)} <- 4
144184
print(${acc('numbers', 1)})`,
145185
);
146186

147187
assertSliced(
148-
label('When there are assignments to only other indices, then only vector is in the slice', basicCapabilities),
188+
label('When there are assignments to only other indices, then only container is in the slice', basicCapabilities),
149189
shell,
150-
`numbers <- c(1, 2, 3)
190+
`numbers <- ${def('1', '2', '3')}
151191
${acc('numbers', 2)} <- 5
152192
${acc('numbers', 3)} <- 6
153193
print(${acc('numbers', 1)})`,
154194
['4@print'],
155-
`numbers <- c(1, 2, 3)
195+
`numbers <- ${def('1', '2', '3')}
156196
print(${acc('numbers', 1)})`,
157197
);
158198

159199
describe('Access within conditionals', () => {
160200
assertSliced(
161201
label('Only a potential overwrite', basicCapabilities),
162202
shell,
163-
`numbers <- c(1)
203+
`numbers <- ${def('1')}
164204
if(u)
165205
${acc('numbers', 1)} <- 2
166206
print(${acc('numbers', 1)})`,
167207
['4@print'],
168-
`numbers <- c(1)
208+
`numbers <- ${def('1')}
169209
if(u) ${acc('numbers', 1)} <- 2
170210
print(${acc('numbers', 1)})`
171211
);
172212

173213
assertSliced(
174214
label('Potential wipe', basicCapabilities),
175215
shell,
176-
`numbers <- c(1)
216+
`numbers <- ${def('1')}
177217
if(u) {
178218
${acc('numbers', 1)} <- 2
179219
} else {
180220
${acc('numbers', 1)} <- 3
181-
numbers <- c()
221+
numbers <- ${def()}
182222
}
183223
print(${acc('numbers', 1)})`,
184224
['8@print'],
185-
`numbers <- c(1)
225+
`numbers <- ${def('1')}
186226
if(u) { ${acc('numbers', 1)} <- 2 } else
187-
{ numbers <- c() }
227+
{ numbers <- ${def()} }
188228
print(${acc('numbers', 1)})`
189229
);
190230
});
@@ -193,14 +233,14 @@ print(${acc('numbers', 1)})`
193233
describe('Config flag', () => {
194234
useConfigForTest({ solver: { pointerTracking: false } });
195235
assertSliced(
196-
label('When flag is false, then vector access is not in slice', ['call-normal']),
236+
label('When flag is false, then container access is not in slice', ['call-normal']),
197237
shell,
198-
`numbers <- c(1, 2)
238+
`numbers <- ${def('1', '2')}
199239
${acc('numbers', 1)} <- 3
200240
${acc('numbers', 2)} <- 4
201241
print(${acc('numbers', 1)})`,
202242
['4@print'],
203-
`numbers <- c(1, 2)
243+
`numbers <- ${def('1', '2')}
204244
${acc('numbers', 1)} <- 3
205245
${acc('numbers', 2)} <- 4
206246
print(${acc('numbers', 1)})`

0 commit comments

Comments
 (0)