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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: jdtang@google.com (Jonathan Tang)
#include "vector.h"
#include <stdlib.h>
#include <string.h>
#include "gtest/gtest.h"
#include "test_utils.h"
namespace {
class GumboVectorTest : public GumboTest {
protected:
GumboVectorTest() : one_(1), two_(2), three_(3) {
gumbo_vector_init(2, &vector_);
}
~GumboVectorTest() {
gumbo_vector_destroy(&vector_);
}
GumboVector vector_;
// dummy ints that we can use to take addresses of.
int one_;
int two_;
int three_;
};
TEST_F(GumboVectorTest, Init) {
EXPECT_EQ(0, vector_.length);
EXPECT_EQ(2, vector_.capacity);
}
TEST_F(GumboVectorTest, InitZeroCapacity) {
gumbo_vector_destroy(&vector_);
gumbo_vector_init(0, &vector_);
gumbo_vector_add(&one_, &vector_);
EXPECT_EQ(1, vector_.length);
EXPECT_EQ(1, *(static_cast<int*>(vector_.data[0])));
}
TEST_F(GumboVectorTest, Add) {
gumbo_vector_add(&one_, &vector_);
EXPECT_EQ(1, vector_.length);
EXPECT_EQ(1, *(static_cast<int*>(vector_.data[0])));
EXPECT_EQ(0, gumbo_vector_index_of(&vector_, &one_));
EXPECT_EQ(-1, gumbo_vector_index_of(&vector_, &two_));
}
TEST_F(GumboVectorTest, AddMultiple) {
gumbo_vector_add(&one_, &vector_);
gumbo_vector_add(&two_, &vector_);
EXPECT_EQ(2, vector_.length);
EXPECT_EQ(2, *(static_cast<int*>(vector_.data[1])));
EXPECT_EQ(1, gumbo_vector_index_of(&vector_, &two_));
}
TEST_F(GumboVectorTest, Realloc) {
gumbo_vector_add(&one_, &vector_);
gumbo_vector_add(&two_, &vector_);
gumbo_vector_add(&three_, &vector_);
EXPECT_EQ(3, vector_.length);
EXPECT_EQ(4, vector_.capacity);
EXPECT_EQ(3, *(static_cast<int*>(vector_.data[2])));
}
TEST_F(GumboVectorTest, Pop) {
gumbo_vector_add(&one_, &vector_);
int result = *static_cast<int*>(gumbo_vector_pop(&vector_));
EXPECT_EQ(1, result);
EXPECT_EQ(0, vector_.length);
}
TEST_F(GumboVectorTest, PopEmpty) {
EXPECT_EQ(NULL, gumbo_vector_pop(&vector_));
}
TEST_F(GumboVectorTest, InsertAtFirst) {
gumbo_vector_add(&one_, &vector_);
gumbo_vector_add(&two_, &vector_);
gumbo_vector_insert_at(&three_, 0, &vector_);
EXPECT_EQ(3, vector_.length);
int result = *static_cast<int*>(vector_.data[0]);
EXPECT_EQ(3, result);
}
TEST_F(GumboVectorTest, InsertAtLast) {
gumbo_vector_add(&one_, &vector_);
gumbo_vector_add(&two_, &vector_);
gumbo_vector_insert_at(&three_, 2, &vector_);
EXPECT_EQ(3, vector_.length);
int result = *static_cast<int*>(vector_.data[2]);
EXPECT_EQ(3, result);
}
TEST_F(GumboVectorTest, Remove) {
gumbo_vector_add(&one_, &vector_);
gumbo_vector_add(&two_, &vector_);
gumbo_vector_add(&three_, &vector_);
gumbo_vector_remove(&two_, &vector_);
EXPECT_EQ(2, vector_.length);
int three = *static_cast<int*>(vector_.data[1]);
EXPECT_EQ(3, three);
}
TEST_F(GumboVectorTest, RemoveAt) {
gumbo_vector_add(&one_, &vector_);
gumbo_vector_add(&two_, &vector_);
gumbo_vector_add(&three_, &vector_);
int result = *static_cast<int*>(
gumbo_vector_remove_at(1, &vector_));
EXPECT_EQ(2, result);
EXPECT_EQ(2, vector_.length);
int three = *static_cast<int*>(vector_.data[1]);
EXPECT_EQ(3, three);
}
} // namespace
|