jdk

Форк
0
/
test_arrayAllocator.cpp 
99 строк · 2.8 Кб
1
/*
2
 * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3
 *
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
 *
6
 * This code is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License version 2 only, as
8
 * published by the Free Software Foundation.
9
 *
10
 * This code is distributed in the hope that it will be useful, but WITHOUT
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
 * version 2 for more details (a copy is included in the LICENSE file that
14
 * accompanied this code).
15
 *
16
 * You should have received a copy of the GNU General Public License version
17
 * 2 along with this work; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
 *
20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
 * or visit www.oracle.com if you need additional information or have any
22
 * questions.
23
 */
24

25
#include "precompiled.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "unittest.hpp"
28

29
using Element = struct TestArrayAllocatorElement {
30
  double a;
31
  int b;
32
};
33

34
static void fill(Element* elements, int start, int size) {
35
  for (int i = 0; i < size; i++) {
36
    new (&elements[start + i]) Element{0.0, start + i};
37
  }
38
}
39

40
static Element* allocate_and_fill(int size) {
41
  Element* const elements = MallocArrayAllocator<Element>::allocate(size, mtTest);
42

43
  fill(elements, 0, size);
44

45
  return elements;
46
}
47

48
TEST_VM(ArrayAllocator, allocate) {
49
  const int size = 10;
50

51
  Element* const elements = allocate_and_fill(size);
52

53
  for (int i = 0; i < size; i++) {
54
    ASSERT_EQ(elements[i].b, i);
55
  }
56

57
  MallocArrayAllocator<Element>::free(elements);
58
}
59

60
TEST_VM(ArrayAllocator, reallocate_0) {
61
  const int size = 10;
62

63
  Element* const elements = allocate_and_fill(size);
64

65
  Element* const ret = MallocArrayAllocator<Element>::reallocate(elements, 0, mtTest);
66
  ASSERT_NE(ret, nullptr) << "We've chosen to NOT return nullptr when reallcting with 0";
67

68
  MallocArrayAllocator<Element>::free(ret);
69
}
70

71
TEST_VM(ArrayAllocator, reallocate_shrink) {
72
  const int size = 10;
73

74
  Element* const elements = allocate_and_fill(size);
75

76
  Element* const ret = MallocArrayAllocator<Element>::reallocate(elements, size / 2, mtTest);
77

78
  for (int i = 0; i < size / 2; i++) {
79
    ASSERT_EQ(ret[i].b, i);
80
  }
81

82
  MallocArrayAllocator<Element>::free(ret);
83
}
84

85
TEST_VM(ArrayAllocator, reallocate_grow) {
86
  const int size = 10;
87

88
  Element* const elements = allocate_and_fill(size);
89

90
  Element* const ret = MallocArrayAllocator<Element>::reallocate(elements, size * 2, mtTest);
91

92
  fill(ret, size, size);
93

94
  for (int i = 0; i < size * 2; i++) {
95
    ASSERT_EQ(ret[i].b, i);
96
  }
97

98
  MallocArrayAllocator<Element>::free(ret);
99
}
100

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.