作者: 谢瑞阳 10225101483 徐翔宇 10225101535
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

  1. // Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors.
  4. #include "util/arena.h"
  5. #include "util/random.h"
  6. #include "util/testharness.h"
  7. namespace leveldb {
  8. class ArenaTest { };
  9. TEST(ArenaTest, Empty) {
  10. Arena arena;
  11. }
  12. TEST(ArenaTest, Simple) {
  13. std::vector<std::pair<size_t, char*> > allocated;
  14. Arena arena;
  15. const int N = 100000;
  16. size_t bytes = 0;
  17. Random rnd(301);
  18. for (int i = 0; i < N; i++) {
  19. size_t s;
  20. if (i % (N / 10) == 0) {
  21. s = i;
  22. } else {
  23. s = rnd.OneIn(4000) ? rnd.Uniform(6000) :
  24. (rnd.OneIn(10) ? rnd.Uniform(100) : rnd.Uniform(20));
  25. }
  26. if (s == 0) {
  27. // Our arena disallows size 0 allocations.
  28. s = 1;
  29. }
  30. char* r;
  31. if (rnd.OneIn(10)) {
  32. r = arena.AllocateAligned(s);
  33. } else {
  34. r = arena.Allocate(s);
  35. }
  36. for (int b = 0; b < s; b++) {
  37. // Fill the "i"th allocation with a known bit pattern
  38. r[b] = i % 256;
  39. }
  40. bytes += s;
  41. allocated.push_back(std::make_pair(s, r));
  42. ASSERT_GE(arena.MemoryUsage(), bytes);
  43. if (i > N/10) {
  44. ASSERT_LE(arena.MemoryUsage(), bytes * 1.10);
  45. }
  46. }
  47. for (int i = 0; i < allocated.size(); i++) {
  48. size_t num_bytes = allocated[i].first;
  49. const char* p = allocated[i].second;
  50. for (int b = 0; b < num_bytes; b++) {
  51. // Check the "i"th allocation for the known bit pattern
  52. ASSERT_EQ(int(p[b]) & 0xff, i % 256);
  53. }
  54. }
  55. }
  56. }
  57. int main(int argc, char** argv) {
  58. return leveldb::test::RunAllTests();
  59. }