小组成员: 曹可心-10223903406 朴祉燕-10224602413
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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