1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/memory_allocator.h"
9 #include "base/macros.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(MemoryAllocatorTest
, NoThrowBuffer
) {
13 const size_t size_list
[] = {0U, 1U, 2U, 11U, 15U, 16U};
15 // Repeat test for different sizes.
16 for (size_t idx
= 0; idx
< arraysize(size_list
); ++idx
) {
17 size_t size
= size_list
[idx
];
19 courgette::NoThrowBuffer
<size_t> buf1
;
20 EXPECT_EQ(0U, buf1
.size());
21 EXPECT_TRUE(buf1
.empty());
23 // Ensure reserve() should not affect size.
24 EXPECT_TRUE(buf1
.reserve(size
/ 2));
25 EXPECT_EQ(0U, buf1
.size());
26 EXPECT_TRUE(buf1
.empty());
28 // Populate with integers from |size| - 1 to 0.
29 for (size_t i
= 0; i
< size
; ++i
) {
30 size_t new_value
= size
- 1 - i
;
31 EXPECT_TRUE(buf1
.push_back(new_value
));
32 EXPECT_EQ(new_value
, buf1
.back());
33 EXPECT_EQ(i
+ 1, buf1
.size());
34 EXPECT_FALSE(buf1
.empty());
37 // Sort, and verify that list is indeed sorted.
38 std::sort(buf1
.begin(), buf1
.end());
39 for (size_t i
= 0; i
< size
; ++i
)
40 EXPECT_EQ(i
, buf1
[i
]);
42 // Test operator[] for read and write.
43 for (size_t i
= 0; i
< size
; ++i
)
44 buf1
[i
] = buf1
[i
] * 2;
47 courgette::NoThrowBuffer
<size_t> buf2
;
50 EXPECT_TRUE(buf2
.append(&buf1
[0], size
));
51 EXPECT_EQ(size
, buf2
.size());
52 for (size_t i
= 0; i
< size
; ++i
)
53 EXPECT_EQ(buf1
[i
], buf2
[i
]);
56 // Test shrinking by resize().
57 const size_t kNewValue
= 137;
58 size_t new_size
= size
/ 2;
59 EXPECT_TRUE(buf2
.resize(new_size
, kNewValue
));
60 EXPECT_EQ(new_size
, buf2
.size());
61 for (size_t i
= 0; i
< new_size
; ++i
)
62 EXPECT_EQ(buf1
[i
], buf2
[i
]);
64 // Test expanding by resize().
65 EXPECT_TRUE(buf2
.resize(size
, kNewValue
));
66 EXPECT_EQ(size
, buf2
.size());
67 for (size_t i
= 0; i
< new_size
; ++i
)
68 EXPECT_EQ(buf1
[i
], buf2
[i
]);
69 for (size_t i
= new_size
; i
< size
; ++i
)
70 EXPECT_EQ(kNewValue
, buf2
[i
]);
74 EXPECT_EQ(0U, buf2
.size());
75 EXPECT_TRUE(buf2
.empty());