aboutsummaryrefslogtreecommitdiff
path: root/unittests/ADT/PackedVectorTest.cpp
blob: 55b5d8d049ddd544dbcaea4bd3fa69270472a7bf (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//===- llvm/unittest/ADT/PackedVectorTest.cpp - PackedVector tests --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// BitVectorTest tests fail on PowerPC for unknown reasons, so disable this
// as well since it depends on a BitVector.
#ifndef __ppc__

#include "llvm/ADT/PackedVector.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

TEST(PackedVectorTest, Operation) {
  PackedVector<unsigned, 2> Vec;
  EXPECT_EQ(0U, Vec.size());
  EXPECT_TRUE(Vec.empty());

  Vec.resize(5);
  EXPECT_EQ(5U, Vec.size());
  EXPECT_FALSE(Vec.empty());

  Vec.resize(11);
  EXPECT_EQ(11U, Vec.size());
  EXPECT_FALSE(Vec.empty());

  PackedVector<unsigned, 2> Vec2(3);
  EXPECT_EQ(3U, Vec2.size());
  EXPECT_FALSE(Vec2.empty());

  Vec.clear();
  EXPECT_EQ(0U, Vec.size());
  EXPECT_TRUE(Vec.empty());

  Vec.push_back(2);
  Vec.push_back(0);
  Vec.push_back(1);
  Vec.push_back(3);

  EXPECT_EQ(2U, Vec[0]);
  EXPECT_EQ(0U, Vec[1]);
  EXPECT_EQ(1U, Vec[2]);
  EXPECT_EQ(3U, Vec[3]);

  EXPECT_FALSE(Vec == Vec2);
  EXPECT_TRUE(Vec != Vec2);

  Vec2.swap(Vec);
  EXPECT_EQ(3U, Vec.size());
  EXPECT_FALSE(Vec.empty());
  EXPECT_EQ(0U, Vec[0]);
  EXPECT_EQ(0U, Vec[1]);
  EXPECT_EQ(0U, Vec[2]);

  EXPECT_EQ(2U, Vec2[0]);
  EXPECT_EQ(0U, Vec2[1]);
  EXPECT_EQ(1U, Vec2[2]);
  EXPECT_EQ(3U, Vec2[3]);

  Vec = Vec2;
  EXPECT_TRUE(Vec == Vec2);
  EXPECT_FALSE(Vec != Vec2);

  Vec[1] = 1;
  Vec2[1] = 2;
  Vec |= Vec2;
  EXPECT_EQ(3U, Vec[1]);
}

#ifdef EXPECT_DEBUG_DEATH

TEST(PackedVectorTest, UnsignedValues) {
  PackedVector<unsigned, 2> Vec(1);
  Vec[0] = 0;
  Vec[0] = 1;
  Vec[0] = 2;
  Vec[0] = 3;
  EXPECT_DEBUG_DEATH(Vec[0] = 4, "value is too big");
  EXPECT_DEBUG_DEATH(Vec[0] = -1, "value is too big");
  EXPECT_DEBUG_DEATH(Vec[0] = 0x100, "value is too big");

  PackedVector<unsigned, 3> Vec2(1);
  Vec2[0] = 0;
  Vec2[0] = 7;
  EXPECT_DEBUG_DEATH(Vec[0] = 8, "value is too big");
}

TEST(PackedVectorTest, SignedValues) {
  PackedVector<signed, 2> Vec(1);
  Vec[0] = -2;
  Vec[0] = -1;
  Vec[0] = 0;
  Vec[0] = 1;
  EXPECT_DEBUG_DEATH(Vec[0] = -3, "value is too big");
  EXPECT_DEBUG_DEATH(Vec[0] = 2, "value is too big");

  PackedVector<signed, 3> Vec2(1);
  Vec2[0] = -4;
  Vec2[0] = 3;
  EXPECT_DEBUG_DEATH(Vec[0] = -5, "value is too big");
  EXPECT_DEBUG_DEATH(Vec[0] = 4, "value is too big");
}

#endif

}

#endif