aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/code_intelligence/jazzer/mutation/mutator/collection/ListMutatorTest.java
blob: 24299f48b0f7223975f10c5b743f33df3e8e93dd (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright 2023 Code Intelligence GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.code_intelligence.jazzer.mutation.mutator.collection;

import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockPseudoRandom;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Collections.emptyList;

import com.code_intelligence.jazzer.mutation.annotation.NotNull;
import com.code_intelligence.jazzer.mutation.annotation.WithSize;
import com.code_intelligence.jazzer.mutation.api.ChainedMutatorFactory;
import com.code_intelligence.jazzer.mutation.api.MutatorFactory;
import com.code_intelligence.jazzer.mutation.api.SerializingMutator;
import com.code_intelligence.jazzer.mutation.mutator.lang.LangMutators;
import com.code_intelligence.jazzer.mutation.support.TestSupport.MockPseudoRandom;
import com.code_intelligence.jazzer.mutation.support.TypeHolder;
import java.lang.reflect.AnnotatedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;

@SuppressWarnings("unchecked")
public class ListMutatorTest {
  public static final MutatorFactory FACTORY =
      new ChainedMutatorFactory(LangMutators.newFactory(), CollectionMutators.newFactory());

  private static SerializingMutator<@NotNull List<@NotNull Integer>> defaultListMutator() {
    AnnotatedType type = new TypeHolder<@NotNull List<@NotNull Integer>>() {}.annotatedType();
    return (SerializingMutator<@NotNull List<@NotNull Integer>>) FACTORY.createOrThrow(type);
  }

  @Test
  void testInit() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();
    assertThat(mutator.toString()).isEqualTo("List<Integer>");

    List<Integer> list;
    try (MockPseudoRandom prng = mockPseudoRandom(
             // targetSize
             1,
             // elementMutator.init
             1)) {
      list = mutator.init(prng);
    }
    assertThat(list).containsExactly(0);
  }

  @Test
  void testInitMaxSize() {
    AnnotatedType type =
        new TypeHolder<@NotNull @WithSize(min = 2, max = 3) List<@NotNull Integer>>(){}
            .annotatedType();

    SerializingMutator<@NotNull List<@NotNull Integer>> mutator =
        (SerializingMutator<@NotNull List<@NotNull Integer>>) FACTORY.createOrThrow(type);

    assertThat(mutator.toString()).isEqualTo("List<Integer>");
    List<Integer> list;
    try (MockPseudoRandom prng = mockPseudoRandom(2, 4, 42L, 4, 43L)) {
      list = mutator.init(prng);
    }

    assertThat(list).containsExactly(42, 43).inOrder();
  }

  @Test
  void testRemoveSingleElement() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // action
             0,
             // number of elements to remove
             1,
             // index to remove
             2)) {
      list = mutator.mutate(list, prng);
    }
    assertThat(list).containsExactly(1, 2, 4, 5, 6, 7, 8, 9).inOrder();
  }

  @Test
  void testRemoveChunk() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // action
             0,
             // chunk size
             2,
             // chunk offset
             3)) {
      list = mutator.mutate(list, prng);
    }
    assertThat(list).containsExactly(1, 2, 3, 6, 7, 8, 9).inOrder();
  }

  @Test
  void testAddSingleElement() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // action
             1,
             // add single element,
             1,
             // offset,
             9,
             // Integral initImpl sentinel value
             4,
             // value
             42L)) {
      list = mutator.mutate(list, prng);
    }
    assertThat(list).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 42).inOrder();
  }

  @Test
  void testAddChunk() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // action
             1,
             // chunkSize
             2,
             // chunkOffset
             3,
             // Integral initImpl
             4,
             // val
             42L)) {
      list = mutator.mutate(list, prng);
    }
    assertThat(list).containsExactly(1, 2, 3, 42, 42, 4, 5, 6, 7, 8, 9).inOrder();
  }

  @Test
  void testChangeSingleElement() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // action
             2,
             // number of elements to mutate
             1,
             // first index to mutate at
             2,
             // mutation choice based on `IntegralMutatorFactory`
             // 2 == closedRange
             2,
             // value
             55L)) {
      list = mutator.mutate(list, prng);
    }
    assertThat(list).containsExactly(1, 2, 55, 4, 5, 6, 7, 8, 9).inOrder();
  }

  @Test
  void testChangeChunk() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // action
             2,
             // number of elements to mutate
             2,
             // first index to mutate at
             5,
             // mutation: 0 == bitflip
             0,
             // shift constant
             13,
             // and again
             0, 12)) {
      list = mutator.mutate(list, prng);
    }
    assertThat(list).containsExactly(1, 2, 3, 4, 5, 8198, 4103, 8, 9, 10, 11).inOrder();
  }

  @Test
  void testCrossOverEmptyLists() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      List<Integer> list = mutator.crossOver(emptyList(), emptyList(), prng);
      assertThat(list).isEmpty();
    }
  }

  @Test
  void testCrossOverInsertChunk() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    List<Integer> otherList =
        new ArrayList<>(Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // insert action
             0,
             // chunk size
             3,
             // fromPos
             2,
             // toPos
             5)) {
      list = mutator.crossOver(list, otherList, prng);
    }
    assertThat(list).containsExactly(0, 1, 2, 3, 4, 12, 13, 14, 5, 6, 7, 8, 9).inOrder();
  }

  @Test
  void testCrossOverOverwriteChunk() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    List<Integer> otherList =
        new ArrayList<>(Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // overwrite action
             1,
             // chunk size
             3,
             // fromPos
             2,
             // toPos
             5)) {
      list = mutator.crossOver(list, otherList, prng);
    }
    assertThat(list).containsExactly(0, 1, 2, 3, 4, 12, 13, 14, 8, 9).inOrder();
  }

  @Test
  void testCrossOverCrossOverChunk() {
    SerializingMutator<@NotNull List<@NotNull Integer>> mutator = defaultListMutator();

    List<Integer> list = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    List<Integer> otherList =
        new ArrayList<>(Arrays.asList(10, 11, 12, 13, 14, 15, 16, 17, 18, 19));
    try (MockPseudoRandom prng = mockPseudoRandom(
             // overwrite action
             2,
             // chunk size
             3,
             // fromPos
             2,
             // toPos
             2,
             // mean value in sub cross over
             0,
             // mean value in sub cross over
             0,
             // mean value in sub cross over
             0)) {
      list = mutator.crossOver(list, otherList, prng);
    }
    assertThat(list).containsExactly(0, 1, 7, 8, 9, 5, 6, 7, 8, 9).inOrder();
  }
}