Skip to content

Commit ca3a60e

Browse files
committed
[Runtime][Test] Add some tests for the heap functions.
We should have some tests for the heap functions. Note that these wouldn't have caught the problem that we fixed in the previous commit, because the conditions under which they run presently mean that the problematic code wouldn't have been active. They will *eventually* test that code, however. rdar://119137861
1 parent 9fc61dd commit ca3a60e

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

unittests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
106106
Metadata.cpp
107107
Enum.cpp
108108
ExtendedExistential.cpp
109+
Heap.cpp
109110
Refcounting.cpp
110111
Stdlib.cpp
111112
StackAllocator.cpp

unittests/runtime/Heap.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===--- Heap.cpp - Heap tests --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Runtime/Heap.h"
14+
15+
#include "gtest/gtest.h"
16+
17+
void shouldAlloc(size_t size, size_t alignMask) {
18+
void *ptr = swift::swift_slowAlloc(size, alignMask);
19+
EXPECT_NE(ptr, (void *)NULL)
20+
<< "Allocation failed for size " << size << " and alignment mask "
21+
<< alignMask << ".";
22+
swift::swift_slowDealloc(ptr, size, alignMask);
23+
}
24+
25+
void shouldAlloc(size_t size) {
26+
shouldAlloc(size, 0);
27+
shouldAlloc(size, 1);
28+
shouldAlloc(size, 3);
29+
shouldAlloc(size, 7);
30+
shouldAlloc(size, 15);
31+
shouldAlloc(size, 31);
32+
shouldAlloc(size, 63);
33+
shouldAlloc(size, 4095);
34+
}
35+
36+
TEST(HeapTest, slowAlloc) {
37+
shouldAlloc(1);
38+
shouldAlloc(8);
39+
shouldAlloc(32);
40+
shouldAlloc(1093);
41+
}
42+
43+
void shouldAllocTyped(size_t size, size_t alignMask, swift::MallocTypeId typeId) {
44+
void *ptr = swift::swift_slowAllocTyped(size, alignMask, typeId);
45+
EXPECT_NE(ptr, (void *)NULL)
46+
<< "Typed allocation failed for size " << size << " and alignment mask "
47+
<< alignMask << ".";
48+
swift::swift_slowDealloc(ptr, size, alignMask);
49+
}
50+
51+
void shouldAllocTyped(size_t size, swift::MallocTypeId typeId) {
52+
shouldAlloc(size, 0);
53+
shouldAlloc(size, 1);
54+
shouldAlloc(size, 3);
55+
shouldAlloc(size, 7);
56+
shouldAlloc(size, 15);
57+
shouldAlloc(size, 31);
58+
shouldAlloc(size, 63);
59+
shouldAlloc(size, 4095);
60+
}
61+
62+
void shouldAllocTyped(size_t size) {
63+
shouldAllocTyped(size, 42);
64+
}
65+
66+
TEST(HeapTest, slowAllocTyped) {
67+
shouldAllocTyped(1);
68+
shouldAllocTyped(8);
69+
shouldAllocTyped(32);
70+
shouldAllocTyped(1093);
71+
}
72+

0 commit comments

Comments
 (0)