1 //===-- sanitizer_common_test.cc ------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "sanitizer_common/sanitizer_common.h"
14 #include "gtest/gtest.h"
15
16 namespace __sanitizer {
17
IsSorted(const uptr * array,uptr n)18 static bool IsSorted(const uptr *array, uptr n) {
19 for (uptr i = 1; i < n; i++) {
20 if (array[i] < array[i - 1]) return false;
21 }
22 return true;
23 }
24
TEST(SanitizerCommon,SortTest)25 TEST(SanitizerCommon, SortTest) {
26 uptr array[100];
27 uptr n = 100;
28 // Already sorted.
29 for (uptr i = 0; i < n; i++) {
30 array[i] = i;
31 }
32 SortArray(array, n);
33 EXPECT_TRUE(IsSorted(array, n));
34 // Reverse order.
35 for (uptr i = 0; i < n; i++) {
36 array[i] = n - 1 - i;
37 }
38 SortArray(array, n);
39 EXPECT_TRUE(IsSorted(array, n));
40 // Mixed order.
41 for (uptr i = 0; i < n; i++) {
42 array[i] = (i % 2 == 0) ? i : n - 1 - i;
43 }
44 SortArray(array, n);
45 EXPECT_TRUE(IsSorted(array, n));
46 // All equal.
47 for (uptr i = 0; i < n; i++) {
48 array[i] = 42;
49 }
50 SortArray(array, n);
51 EXPECT_TRUE(IsSorted(array, n));
52 // All but one sorted.
53 for (uptr i = 0; i < n - 1; i++) {
54 array[i] = i;
55 }
56 array[n - 1] = 42;
57 SortArray(array, n);
58 EXPECT_TRUE(IsSorted(array, n));
59 // Minimal case - sort three elements.
60 array[0] = 1;
61 array[1] = 0;
62 SortArray(array, 2);
63 EXPECT_TRUE(IsSorted(array, 2));
64 }
65
66 } // namespace sanitizer
67