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 | 20 |
21 Dart_Port port = 1; | 21 Dart_Port port = 1; |
22 | 22 |
23 const char* str1 = "msg1"; | 23 const char* str1 = "msg1"; |
24 const char* str2 = "msg2"; | 24 const char* str2 = "msg2"; |
| 25 const char* str3 = "msg3"; |
| 26 const char* str4 = "msg4"; |
| 27 const char* str5 = "msg5"; |
| 28 const char* str6 = "msg6"; |
25 | 29 |
26 // Add two messages. | 30 // Add two messages. |
27 Message* msg1 = | 31 Message* msg1 = new Message( |
28 new Message(port, AllocMsg(str1), strlen(str1) + 1, | 32 port, AllocMsg(str1), strlen(str1) + 1, Message::kNormalPriority); |
29 Message::kNormalPriority); | 33 queue.Enqueue(msg1, false); |
30 queue.Enqueue(msg1); | |
31 EXPECT(!queue.IsEmpty()); | 34 EXPECT(!queue.IsEmpty()); |
32 | 35 |
33 Message* msg2 = | 36 Message* msg2 = new Message( |
34 new Message(port, AllocMsg(str2), strlen(str2) + 1, | 37 port, AllocMsg(str2), strlen(str2) + 1, Message::kNormalPriority); |
35 Message::kNormalPriority); | 38 queue.Enqueue(msg2, false); |
36 | |
37 queue.Enqueue(msg2); | |
38 EXPECT(!queue.IsEmpty()); | 39 EXPECT(!queue.IsEmpty()); |
39 | 40 |
40 // Remove two messages. | 41 // Remove two messages. |
41 Message* msg = queue.Dequeue(); | 42 Message* msg = queue.Dequeue(); |
42 EXPECT(msg != NULL); | 43 EXPECT(msg != NULL); |
43 EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->data())); | 44 EXPECT_STREQ(str1, reinterpret_cast<char*>(msg->data())); |
44 EXPECT(!queue.IsEmpty()); | 45 EXPECT(!queue.IsEmpty()); |
45 | 46 |
46 msg = queue.Dequeue(); | 47 msg = queue.Dequeue(); |
47 EXPECT(msg != NULL); | 48 EXPECT(msg != NULL); |
48 EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->data())); | 49 EXPECT_STREQ(str2, reinterpret_cast<char*>(msg->data())); |
49 EXPECT(queue.IsEmpty()); | 50 EXPECT(queue.IsEmpty()); |
50 | 51 |
| 52 Message* msg3 = new Message(Message::kIllegalPort, |
| 53 AllocMsg(str3), strlen(str3) + 1, |
| 54 Message::kNormalPriority); |
| 55 queue.Enqueue(msg3, true); |
| 56 EXPECT(!queue.IsEmpty()); |
| 57 |
| 58 Message* msg4 = new Message(Message::kIllegalPort, |
| 59 AllocMsg(str4), strlen(str4) + 1, |
| 60 Message::kNormalPriority); |
| 61 queue.Enqueue(msg4, true); |
| 62 EXPECT(!queue.IsEmpty()); |
| 63 |
| 64 Message* msg5 = new Message( |
| 65 port, AllocMsg(str5), strlen(str5) + 1, Message::kNormalPriority); |
| 66 queue.Enqueue(msg5, false); |
| 67 EXPECT(!queue.IsEmpty()); |
| 68 |
| 69 Message* msg6 = new Message(Message::kIllegalPort, |
| 70 AllocMsg(str6), strlen(str6) + 1, |
| 71 Message::kNormalPriority); |
| 72 queue.Enqueue(msg6, true); |
| 73 EXPECT(!queue.IsEmpty()); |
| 74 |
| 75 msg = queue.Dequeue(); |
| 76 EXPECT(msg != NULL); |
| 77 EXPECT_STREQ(str3, reinterpret_cast<char*>(msg->data())); |
| 78 EXPECT(!queue.IsEmpty()); |
| 79 |
| 80 msg = queue.Dequeue(); |
| 81 EXPECT(msg != NULL); |
| 82 EXPECT_STREQ(str4, reinterpret_cast<char*>(msg->data())); |
| 83 EXPECT(!queue.IsEmpty()); |
| 84 |
| 85 msg = queue.Dequeue(); |
| 86 EXPECT(msg != NULL); |
| 87 EXPECT_STREQ(str6, reinterpret_cast<char*>(msg->data())); |
| 88 EXPECT(!queue.IsEmpty()); |
| 89 |
| 90 msg = queue.Dequeue(); |
| 91 EXPECT(msg != NULL); |
| 92 EXPECT_STREQ(str5, reinterpret_cast<char*>(msg->data())); |
| 93 EXPECT(queue.IsEmpty()); |
| 94 |
51 delete msg1; | 95 delete msg1; |
52 delete msg2; | 96 delete msg2; |
| 97 delete msg3; |
| 98 delete msg4; |
| 99 delete msg5; |
| 100 delete msg6; |
53 } | 101 } |
54 | 102 |
55 | 103 |
56 TEST_CASE(MessageQueue_Clear) { | 104 TEST_CASE(MessageQueue_Clear) { |
57 MessageQueue queue; | 105 MessageQueue queue; |
58 Dart_Port port1 = 1; | 106 Dart_Port port1 = 1; |
59 Dart_Port port2 = 2; | 107 Dart_Port port2 = 2; |
60 | 108 |
61 const char* str1 = "msg1"; | 109 const char* str1 = "msg1"; |
62 const char* str2 = "msg2"; | 110 const char* str2 = "msg2"; |
63 | 111 |
64 // Add two messages. | 112 // Add two messages. |
65 Message* msg1 = | 113 Message* msg1 = |
66 new Message(port1, AllocMsg(str1), strlen(str1) + 1, | 114 new Message(port1, AllocMsg(str1), strlen(str1) + 1, |
67 Message::kNormalPriority); | 115 Message::kNormalPriority); |
68 queue.Enqueue(msg1); | 116 queue.Enqueue(msg1, false); |
69 Message* msg2 = | 117 Message* msg2 = |
70 new Message(port2, AllocMsg(str2), strlen(str2) + 1, | 118 new Message(port2, AllocMsg(str2), strlen(str2) + 1, |
71 Message::kNormalPriority); | 119 Message::kNormalPriority); |
72 queue.Enqueue(msg2); | 120 queue.Enqueue(msg2, false); |
73 | 121 |
74 EXPECT(!queue.IsEmpty()); | 122 EXPECT(!queue.IsEmpty()); |
75 queue.Clear(); | 123 queue.Clear(); |
76 EXPECT(queue.IsEmpty()); | 124 EXPECT(queue.IsEmpty()); |
77 | 125 |
78 // msg1 and msg2 already delete by FlushAll. | 126 // msg1 and msg2 already delete by FlushAll. |
79 } | 127 } |
80 | 128 |
81 } // namespace dart | 129 } // namespace dart |
OLD | NEW |