OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include <stdlib.h> |
| 29 #include <utility> |
| 30 |
| 31 #include "src/v8.h" |
| 32 |
| 33 #include "test/cctest/cctest.h" |
| 34 |
| 35 using namespace v8::internal; |
| 36 |
| 37 TEST(RingBufferPartialFill) { |
| 38 const int max_size = 6; |
| 39 typedef RingBuffer<int, max_size>::const_iterator Iter; |
| 40 RingBuffer<int, max_size> ring_buffer; |
| 41 CHECK(ring_buffer.empty()); |
| 42 CHECK_EQ(static_cast<int>(ring_buffer.size()), 0); |
| 43 CHECK(ring_buffer.begin() == ring_buffer.end()); |
| 44 |
| 45 // Fill ring_buffer partially: [0, 1, 2] |
| 46 for (int i = 0; i < max_size / 2; i++) ring_buffer.push_back(i); |
| 47 |
| 48 CHECK(!ring_buffer.empty()); |
| 49 CHECK(static_cast<int>(ring_buffer.size()) == max_size / 2); |
| 50 CHECK(ring_buffer.begin() != ring_buffer.end()); |
| 51 |
| 52 // Test forward itartion |
| 53 int i = 0; |
| 54 for (Iter iter = ring_buffer.begin(); iter != ring_buffer.end(); ++iter) { |
| 55 CHECK(*iter == i); |
| 56 ++i; |
| 57 } |
| 58 CHECK_EQ(i, 3); // one past last element. |
| 59 |
| 60 // Test backward iteration |
| 61 i = 2; |
| 62 Iter iter = ring_buffer.back(); |
| 63 while (true) { |
| 64 CHECK(*iter == i); |
| 65 if (iter == ring_buffer.begin()) break; |
| 66 --iter; |
| 67 --i; |
| 68 } |
| 69 CHECK_EQ(i, 0); |
| 70 } |
| 71 |
| 72 |
| 73 TEST(RingBufferWrapAround) { |
| 74 const int max_size = 6; |
| 75 typedef RingBuffer<int, max_size>::const_iterator Iter; |
| 76 RingBuffer<int, max_size> ring_buffer; |
| 77 |
| 78 // Fill ring_buffer (wrap around): [9, 10, 11, 12, 13, 14] |
| 79 for (int i = 0; i < 2 * max_size + 3; i++) ring_buffer.push_back(i); |
| 80 |
| 81 CHECK(!ring_buffer.empty()); |
| 82 CHECK(static_cast<int>(ring_buffer.size()) == max_size); |
| 83 CHECK(ring_buffer.begin() != ring_buffer.end()); |
| 84 |
| 85 // Test forward iteration |
| 86 int i = 9; |
| 87 for (Iter iter = ring_buffer.begin(); iter != ring_buffer.end(); ++iter) { |
| 88 CHECK(*iter == i); |
| 89 ++i; |
| 90 } |
| 91 CHECK_EQ(i, 15); // one past last element. |
| 92 |
| 93 // Test backward iteration |
| 94 i = 14; |
| 95 Iter iter = ring_buffer.back(); |
| 96 while (true) { |
| 97 CHECK(*iter == i); |
| 98 if (iter == ring_buffer.begin()) break; |
| 99 --iter; |
| 100 --i; |
| 101 } |
| 102 CHECK_EQ(i, 9); |
| 103 } |
| 104 |
| 105 |
| 106 TEST(RingBufferPushFront) { |
| 107 const int max_size = 6; |
| 108 typedef RingBuffer<int, max_size>::const_iterator Iter; |
| 109 RingBuffer<int, max_size> ring_buffer; |
| 110 |
| 111 // Fill ring_buffer (wrap around): [14, 13, 12, 11, 10, 9] |
| 112 for (int i = 0; i < 2 * max_size + 3; i++) ring_buffer.push_front(i); |
| 113 |
| 114 CHECK(!ring_buffer.empty()); |
| 115 CHECK(static_cast<int>(ring_buffer.size()) == max_size); |
| 116 CHECK(ring_buffer.begin() != ring_buffer.end()); |
| 117 |
| 118 // Test forward iteration |
| 119 int i = 14; |
| 120 for (Iter iter = ring_buffer.begin(); iter != ring_buffer.end(); ++iter) { |
| 121 CHECK(*iter == i); |
| 122 --i; |
| 123 } |
| 124 CHECK_EQ(i, 8); // one past last element. |
| 125 } |
OLD | NEW |