OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // |
| 3 // Tests of circular queues. |
| 4 |
| 5 #include "v8.h" |
| 6 #include "circular-queue-inl.h" |
| 7 #include "cctest.h" |
| 8 |
| 9 namespace i = v8::internal; |
| 10 |
| 11 using i::CircularQueue; |
| 12 using i::SamplingCircularQueue; |
| 13 |
| 14 |
| 15 TEST(SingleRecordCircularQueue) { |
| 16 typedef int Record; |
| 17 CircularQueue<Record> cq(sizeof(Record) * 2); |
| 18 CHECK(cq.IsEmpty()); |
| 19 cq.Enqueue(1); |
| 20 CHECK(!cq.IsEmpty()); |
| 21 Record rec = 0; |
| 22 cq.Dequeue(&rec); |
| 23 CHECK_EQ(1, rec); |
| 24 CHECK(cq.IsEmpty()); |
| 25 } |
| 26 |
| 27 |
| 28 TEST(MultipleRecordsCircularQueue) { |
| 29 typedef int Record; |
| 30 const int kQueueSize = 10; |
| 31 CircularQueue<Record> cq(sizeof(Record) * (kQueueSize + 1)); |
| 32 CHECK(cq.IsEmpty()); |
| 33 cq.Enqueue(1); |
| 34 CHECK(!cq.IsEmpty()); |
| 35 for (int i = 2; i <= 5; ++i) { |
| 36 cq.Enqueue(i); |
| 37 CHECK(!cq.IsEmpty()); |
| 38 } |
| 39 Record rec = 0; |
| 40 for (int i = 1; i <= 4; ++i) { |
| 41 CHECK(!cq.IsEmpty()); |
| 42 cq.Dequeue(&rec); |
| 43 CHECK_EQ(i, rec); |
| 44 } |
| 45 for (int i = 6; i <= 12; ++i) { |
| 46 cq.Enqueue(i); |
| 47 CHECK(!cq.IsEmpty()); |
| 48 } |
| 49 for (int i = 5; i <= 12; ++i) { |
| 50 CHECK(!cq.IsEmpty()); |
| 51 cq.Dequeue(&rec); |
| 52 CHECK_EQ(i, rec); |
| 53 } |
| 54 CHECK(cq.IsEmpty()); |
| 55 } |
| 56 |
| 57 |
| 58 TEST(SamplingCircularQueue) { |
| 59 typedef int Record; |
| 60 const int kRecordsPerChunk = 4; |
| 61 SamplingCircularQueue scq(sizeof(Record), |
| 62 kRecordsPerChunk * sizeof(Record), |
| 63 3); |
| 64 scq.SetUpProducer(); |
| 65 scq.SetUpConsumer(); |
| 66 |
| 67 // Check that we are using non-reserved values. |
| 68 CHECK_NE(SamplingCircularQueue::kClear, 1); |
| 69 CHECK_NE(SamplingCircularQueue::kEnd, 1); |
| 70 // Fill up the first chunk. |
| 71 CHECK_EQ(NULL, scq.StartDequeue()); |
| 72 for (int i = 1; i < 1 + kRecordsPerChunk; ++i) { |
| 73 Record* rec = reinterpret_cast<Record*>(scq.Enqueue()); |
| 74 CHECK_NE(NULL, rec); |
| 75 *rec = i; |
| 76 CHECK_EQ(NULL, scq.StartDequeue()); |
| 77 } |
| 78 |
| 79 // Fill up the second chunk. Consumption must still be unavailable. |
| 80 CHECK_EQ(NULL, scq.StartDequeue()); |
| 81 for (int i = 10; i < 10 + kRecordsPerChunk; ++i) { |
| 82 Record* rec = reinterpret_cast<Record*>(scq.Enqueue()); |
| 83 CHECK_NE(NULL, rec); |
| 84 *rec = i; |
| 85 CHECK_EQ(NULL, scq.StartDequeue()); |
| 86 } |
| 87 |
| 88 Record* rec = reinterpret_cast<Record*>(scq.Enqueue()); |
| 89 CHECK_NE(NULL, rec); |
| 90 *rec = 20; |
| 91 // Now as we started filling up the third chunk, consumption |
| 92 // must become possible. |
| 93 CHECK_NE(NULL, scq.StartDequeue()); |
| 94 |
| 95 // Consume the first chunk. |
| 96 for (int i = 1; i < 1 + kRecordsPerChunk; ++i) { |
| 97 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); |
| 98 CHECK_NE(NULL, rec); |
| 99 CHECK_EQ(i, *rec); |
| 100 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); |
| 101 scq.FinishDequeue(); |
| 102 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); |
| 103 } |
| 104 // Now consumption must not be possible, as consumer now polls |
| 105 // the first chunk for emptinness. |
| 106 CHECK_EQ(NULL, scq.StartDequeue()); |
| 107 |
| 108 scq.FlushResidualRecords(); |
| 109 // From now, consumer no more polls ahead of the current chunk, |
| 110 // so it's possible to consume the second chunk. |
| 111 CHECK_NE(NULL, scq.StartDequeue()); |
| 112 // Consume the second chunk |
| 113 for (int i = 10; i < 10 + kRecordsPerChunk; ++i) { |
| 114 Record* rec = reinterpret_cast<Record*>(scq.StartDequeue()); |
| 115 CHECK_NE(NULL, rec); |
| 116 CHECK_EQ(i, *rec); |
| 117 CHECK_EQ(rec, reinterpret_cast<Record*>(scq.StartDequeue())); |
| 118 scq.FinishDequeue(); |
| 119 CHECK_NE(rec, reinterpret_cast<Record*>(scq.StartDequeue())); |
| 120 } |
| 121 // Consumption must still be possible as the first cell of the |
| 122 // last chunk is not clean. |
| 123 CHECK_NE(NULL, scq.StartDequeue()); |
| 124 |
| 125 scq.TearDownConsumer(); |
| 126 scq.TearDownProducer(); |
| 127 } |
OLD | NEW |