vector.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: jdtang@google.com (Jonathan Tang)
  16. #include "vector.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "gtest/gtest.h"
  20. #include "test_utils.h"
  21. namespace {
  22. class GumboVectorTest : public GumboTest {
  23. protected:
  24. GumboVectorTest()
  25. : one_(1),
  26. two_(2),
  27. three_(3),
  28. num_allocations_(0),
  29. bytes_allocated_(0),
  30. num_deallocations_(0) {
  31. gumbo_vector_init(&parser_, 2, &vector_);
  32. }
  33. ~GumboVectorTest() { gumbo_vector_destroy(&parser_, &vector_); }
  34. GumboVector vector_;
  35. // dummy ints that we can use to take addresses of.
  36. int one_;
  37. int two_;
  38. int three_;
  39. // Counters for testing a custom allocator.
  40. int num_allocations_;
  41. int bytes_allocated_;
  42. int num_deallocations_;
  43. static void* custom_gumbo_vector_allocator(void* userdata, size_t num_bytes) {
  44. GumboVectorTest* test = static_cast<GumboVectorTest*>(userdata);
  45. ++test->num_allocations_;
  46. test->bytes_allocated_ += num_bytes;
  47. return malloc(num_bytes);
  48. }
  49. static void custom_gumbo_vector_deallocator(void* userdata, void* ptr) {
  50. GumboVectorTest* test = static_cast<GumboVectorTest*>(userdata);
  51. ++test->num_deallocations_;
  52. free(ptr);
  53. }
  54. };
  55. TEST_F(GumboVectorTest, Init) {
  56. EXPECT_EQ(0, vector_.length);
  57. EXPECT_EQ(2, vector_.capacity);
  58. }
  59. TEST_F(GumboVectorTest, InitZeroCapacity) {
  60. gumbo_vector_destroy(&parser_, &vector_);
  61. gumbo_vector_init(&parser_, 0, &vector_);
  62. gumbo_vector_add(&parser_, &one_, &vector_);
  63. EXPECT_EQ(1, vector_.length);
  64. EXPECT_EQ(1, *(static_cast<int*>(vector_.data[0])));
  65. }
  66. TEST_F(GumboVectorTest, Add) {
  67. gumbo_vector_add(&parser_, &one_, &vector_);
  68. EXPECT_EQ(1, vector_.length);
  69. EXPECT_EQ(1, *(static_cast<int*>(vector_.data[0])));
  70. EXPECT_EQ(0, gumbo_vector_index_of(&vector_, &one_));
  71. EXPECT_EQ(-1, gumbo_vector_index_of(&vector_, &two_));
  72. }
  73. TEST_F(GumboVectorTest, AddMultiple) {
  74. gumbo_vector_add(&parser_, &one_, &vector_);
  75. gumbo_vector_add(&parser_, &two_, &vector_);
  76. EXPECT_EQ(2, vector_.length);
  77. EXPECT_EQ(2, *(static_cast<int*>(vector_.data[1])));
  78. EXPECT_EQ(1, gumbo_vector_index_of(&vector_, &two_));
  79. }
  80. TEST_F(GumboVectorTest, Realloc) {
  81. gumbo_vector_add(&parser_, &one_, &vector_);
  82. gumbo_vector_add(&parser_, &two_, &vector_);
  83. gumbo_vector_add(&parser_, &three_, &vector_);
  84. EXPECT_EQ(3, vector_.length);
  85. EXPECT_EQ(4, vector_.capacity);
  86. EXPECT_EQ(3, *(static_cast<int*>(vector_.data[2])));
  87. }
  88. TEST_F(GumboVectorTest, Pop) {
  89. gumbo_vector_add(&parser_, &one_, &vector_);
  90. int result = *static_cast<int*>(gumbo_vector_pop(&parser_, &vector_));
  91. EXPECT_EQ(1, result);
  92. EXPECT_EQ(0, vector_.length);
  93. }
  94. TEST_F(GumboVectorTest, PopEmpty) {
  95. EXPECT_EQ(NULL, gumbo_vector_pop(&parser_, &vector_));
  96. }
  97. TEST_F(GumboVectorTest, InsertAtFirst) {
  98. gumbo_vector_add(&parser_, &one_, &vector_);
  99. gumbo_vector_add(&parser_, &two_, &vector_);
  100. gumbo_vector_insert_at(&parser_, &three_, 0, &vector_);
  101. EXPECT_EQ(3, vector_.length);
  102. int result = *static_cast<int*>(vector_.data[0]);
  103. EXPECT_EQ(3, result);
  104. }
  105. TEST_F(GumboVectorTest, InsertAtLast) {
  106. gumbo_vector_add(&parser_, &one_, &vector_);
  107. gumbo_vector_add(&parser_, &two_, &vector_);
  108. gumbo_vector_insert_at(&parser_, &three_, 2, &vector_);
  109. EXPECT_EQ(3, vector_.length);
  110. int result = *static_cast<int*>(vector_.data[2]);
  111. EXPECT_EQ(3, result);
  112. }
  113. TEST_F(GumboVectorTest, Remove) {
  114. gumbo_vector_add(&parser_, &one_, &vector_);
  115. gumbo_vector_add(&parser_, &two_, &vector_);
  116. gumbo_vector_add(&parser_, &three_, &vector_);
  117. gumbo_vector_remove(&parser_, &two_, &vector_);
  118. EXPECT_EQ(2, vector_.length);
  119. int three = *static_cast<int*>(vector_.data[1]);
  120. EXPECT_EQ(3, three);
  121. }
  122. TEST_F(GumboVectorTest, RemoveAt) {
  123. gumbo_vector_add(&parser_, &one_, &vector_);
  124. gumbo_vector_add(&parser_, &two_, &vector_);
  125. gumbo_vector_add(&parser_, &three_, &vector_);
  126. int result =
  127. *static_cast<int*>(gumbo_vector_remove_at(&parser_, 1, &vector_));
  128. EXPECT_EQ(2, result);
  129. EXPECT_EQ(2, vector_.length);
  130. int three = *static_cast<int*>(vector_.data[1]);
  131. EXPECT_EQ(3, three);
  132. }
  133. } // namespace