forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.cpp
More file actions
65 lines (55 loc) · 1.68 KB
/
Algorithms.cpp
File metadata and controls
65 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/Support/Algorithms.h"
#include "gtest/gtest.h"
#include <array>
#include <type_traits>
using namespace hermes;
namespace {
struct NontrivialType {
static int kConstructorCalls;
static int kAssignmentCalls;
int x;
NontrivialType() {
x = 17;
kConstructorCalls++;
}
NontrivialType(const NontrivialType &) {
x = 18;
kConstructorCalls++;
}
NontrivialType &operator=(const NontrivialType &) {
x = 19;
kAssignmentCalls++;
return *this;
}
};
int NontrivialType::kConstructorCalls = 0;
int NontrivialType::kAssignmentCalls = 0;
} // namespace
TEST(Algorithms, Copy) {
NontrivialType::kConstructorCalls = 0;
NontrivialType::kAssignmentCalls = 0;
EXPECT_FALSE(std::is_trivial<NontrivialType>::value);
std::array<NontrivialType, 1> nts;
EXPECT_EQ(1, NontrivialType::kConstructorCalls);
EXPECT_EQ(0, NontrivialType::kAssignmentCalls);
// Use malloc() to get uninitialized memory.
NontrivialType *ptr = static_cast<NontrivialType *>(malloc(sizeof *ptr));
// uninitializedCopy should not invoke assignment.
hermes::uninitializedCopy(nts.begin(), nts.end(), ptr);
EXPECT_EQ(2, NontrivialType::kConstructorCalls);
EXPECT_EQ(0, NontrivialType::kAssignmentCalls);
EXPECT_EQ(ptr->x, 18);
// uninitializedCopyN should not invoke assignment.
nts[0].x = 100;
hermes::uninitializedCopyN(nts.begin(), 1, ptr);
EXPECT_EQ(3, NontrivialType::kConstructorCalls);
EXPECT_EQ(0, NontrivialType::kAssignmentCalls);
EXPECT_EQ(ptr->x, 18);
free(ptr);
}