OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/message.h" | 6 #include "vm/message.h" |
7 #include "vm/unit_test.h" | 7 #include "vm/unit_test.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
11 | 11 |
12 static uint8_t* AllocMsg(const char* str) { | 12 static uint8_t* AllocMsg(const char* str) { |
13 return reinterpret_cast<uint8_t*>(strdup(str)); | 13 return reinterpret_cast<uint8_t*>(strdup(str)); |
14 } | 14 } |
15 | 15 |
16 | 16 |
17 TEST_CASE(MessageQueue_BasicOperations) { | 17 TEST_CASE(MessageQueue_BasicOperations) { |
18 MessageQueue queue; | 18 MessageQueue queue; |
19 EXPECT(queue.IsEmpty()); | 19 EXPECT(queue.IsEmpty()); |
| 20 MessageQueue::Iterator it(&queue); |
| 21 // Queue is empty. |
| 22 EXPECT(!it.HasNext()); |
20 | 23 |
21 Dart_Port port = 1; | 24 Dart_Port port = 1; |
22 | 25 |
23 const char* str1 = "msg1"; | 26 const char* str1 = "msg1"; |
24 const char* str2 = "msg2"; | 27 const char* str2 = "msg2"; |
25 const char* str3 = "msg3"; | 28 const char* str3 = "msg3"; |
26 const char* str4 = "msg4"; | 29 const char* str4 = "msg4"; |
27 const char* str5 = "msg5"; | 30 const char* str5 = "msg5"; |
28 const char* str6 = "msg6"; | 31 const char* str6 = "msg6"; |
29 | 32 |
30 // Add two messages. | 33 // Add two messages. |
31 Message* msg1 = new Message( | 34 Message* msg1 = new Message( |
32 port, AllocMsg(str1), strlen(str1) + 1, Message::kNormalPriority); | 35 port, AllocMsg(str1), strlen(str1) + 1, Message::kNormalPriority); |
33 queue.Enqueue(msg1, false); | 36 queue.Enqueue(msg1, false); |
| 37 EXPECT(queue.Length() == 1); |
34 EXPECT(!queue.IsEmpty()); | 38 EXPECT(!queue.IsEmpty()); |
| 39 it.Reset(&queue); |
| 40 EXPECT(it.HasNext()); |
| 41 EXPECT(it.Next() == msg1); |
| 42 EXPECT(!it.HasNext()); |
35 | 43 |
36 Message* msg2 = new Message( | 44 Message* msg2 = new Message( |
37 port, AllocMsg(str2), strlen(str2) + 1, Message::kNormalPriority); | 45 port, AllocMsg(str2), strlen(str2) + 1, Message::kNormalPriority); |
38 queue.Enqueue(msg2, false); | 46 queue.Enqueue(msg2, false); |
| 47 EXPECT(queue.Length() == 2); |
39 EXPECT(!queue.IsEmpty()); | 48 EXPECT(!queue.IsEmpty()); |
| 49 it.Reset(&queue); |
| 50 EXPECT(it.HasNext()); |
| 51 EXPECT(it.Next() == msg1); |
| 52 EXPECT(it.HasNext()); |
| 53 EXPECT(it.Next() == msg2); |
| 54 EXPECT(!it.HasNext()); |
40 | 55 |
41 // Remove two messages. | 56 // Lookup messages by id. |
| 57 EXPECT(queue.FindMessageById(reinterpret_cast<intptr_t>(msg1)) == msg1); |
| 58 EXPECT(queue.FindMessageById(reinterpret_cast<intptr_t>(msg2)) == msg2); |
| 59 |
| 60 // Lookup bad id. |
| 61 EXPECT(queue.FindMessageById(0x1) == NULL); |
| 62 |
| 63 // Remove message 1 |
42 Message* msg = queue.Dequeue(); | 64 Message* msg = queue.Dequeue(); |
43 EXPECT(msg != NULL); | 65 EXPECT(msg != NULL); |
44 EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->data())); | 66 EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->data())); |
45 EXPECT(!queue.IsEmpty()); | 67 EXPECT(!queue.IsEmpty()); |
46 | 68 |
| 69 it.Reset(&queue); |
| 70 EXPECT(it.HasNext()); |
| 71 EXPECT(it.Next() == msg2); |
| 72 |
| 73 // Remove message 2 |
47 msg = queue.Dequeue(); | 74 msg = queue.Dequeue(); |
48 EXPECT(msg != NULL); | 75 EXPECT(msg != NULL); |
49 EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->data())); | 76 EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->data())); |
50 EXPECT(queue.IsEmpty()); | 77 EXPECT(queue.IsEmpty()); |
51 | 78 |
52 Message* msg3 = new Message(Message::kIllegalPort, | 79 Message* msg3 = new Message(Message::kIllegalPort, |
53 AllocMsg(str3), strlen(str3) + 1, | 80 AllocMsg(str3), strlen(str3) + 1, |
54 Message::kNormalPriority); | 81 Message::kNormalPriority); |
55 queue.Enqueue(msg3, true); | 82 queue.Enqueue(msg3, true); |
56 EXPECT(!queue.IsEmpty()); | 83 EXPECT(!queue.IsEmpty()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 queue.Enqueue(msg2, false); | 147 queue.Enqueue(msg2, false); |
121 | 148 |
122 EXPECT(!queue.IsEmpty()); | 149 EXPECT(!queue.IsEmpty()); |
123 queue.Clear(); | 150 queue.Clear(); |
124 EXPECT(queue.IsEmpty()); | 151 EXPECT(queue.IsEmpty()); |
125 | 152 |
126 // msg1 and msg2 already delete by FlushAll. | 153 // msg1 and msg2 already delete by FlushAll. |
127 } | 154 } |
128 | 155 |
129 } // namespace dart | 156 } // namespace dart |
OLD | NEW |