|
| 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