OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | |
2 // | |
3 // Tests of logging utilities from log-utils.h | |
4 | |
5 #ifdef ENABLE_LOGGING_AND_PROFILING | |
6 | |
7 #include "v8.h" | |
8 | |
9 #include "log-utils.h" | |
10 #include "cctest.h" | |
11 | |
12 using v8::internal::EmbeddedVector; | |
13 using v8::internal::LogDynamicBuffer; | |
14 using v8::internal::Vector; | |
15 | |
16 // Writes ref_buffer.length chars into dynabuf. | |
Søren Thygesen Gjesse
2009/05/27 12:00:40
Please add to comment that this also actually fill
Mikhail Naganov
2009/05/28 07:05:35
Done.
| |
17 // The data written is a sequence of two-digit hex numbers: '0001020304...'. | |
18 static void WriteData(LogDynamicBuffer* dynabuf, Vector<char>* ref_buffer) { | |
19 static const char kHex[] = "0123456789ABCDEF"; | |
20 CHECK_GT(ref_buffer->length(), 0); | |
21 CHECK_GT(513, ref_buffer->length()); | |
22 for (int i = 0, half_len = ref_buffer->length() >> 1; i < half_len; ++i) { | |
23 (*ref_buffer)[i << 1] = kHex[i >> 4]; | |
24 (*ref_buffer)[(i << 1) + 1] = kHex[i & 15]; | |
25 } | |
26 if (ref_buffer->length() & 1) { | |
27 ref_buffer->last() = kHex[ref_buffer->length() >> 5]; | |
28 } | |
29 CHECK_EQ(ref_buffer->length(), | |
30 dynabuf->Write(ref_buffer->start(), ref_buffer->length())); | |
31 } | |
32 | |
33 | |
34 static int ReadData( | |
35 LogDynamicBuffer* dynabuf, int start_pos, i::Vector<char>* buffer) { | |
36 return dynabuf->Read(start_pos, buffer->start(), buffer->length()); | |
37 } | |
38 | |
39 | |
40 // Helper function used by CHECK_EQ to compare Vectors. | |
41 static inline void CheckEqualsHelper(const char* file, int line, | |
42 const char* expected_source, | |
43 const Vector<char>& expected, | |
44 const char* value_source, | |
45 const Vector<char>& value) { | |
46 if (expected.length() != value.length()) { | |
47 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n" | |
48 "# Vectors lengths differ: %d expected, %d found", | |
49 expected_source, value_source, | |
50 expected.length(), value.length()); | |
51 } | |
52 if (strncmp(expected.start(), value.start(), expected.length()) != 0) { | |
53 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n" | |
54 "# Vectors contents differ:\n" | |
55 "# Expected: %.*s\n" | |
56 "# Found: %.*s", | |
57 expected_source, value_source, | |
58 expected.length(), expected.start(), | |
59 value.length(), value.start()); | |
60 } | |
61 } | |
62 | |
63 | |
64 TEST(DynaBufSingleBlock) { | |
65 LogDynamicBuffer dynabuf(32, 32); | |
66 EmbeddedVector<char, 32> ref_buf; | |
67 WriteData(&dynabuf, &ref_buf); | |
68 EmbeddedVector<char, 32> buf; | |
69 CHECK_EQ(32, dynabuf.Read(0, buf.start(), buf.length())); | |
70 CHECK_EQ(32, ReadData(&dynabuf, 0, &buf)); | |
71 CHECK_EQ(ref_buf, buf); | |
72 | |
73 // Verify that we can't read and write past the end. | |
74 CHECK_EQ(0, dynabuf.Read(32, buf.start(), buf.length())); | |
75 CHECK_EQ(0, dynabuf.Write(buf.start(), buf.length())); | |
76 } | |
77 | |
78 | |
79 TEST(DynaBufCrossBlocks) { | |
80 LogDynamicBuffer dynabuf(32, 128); | |
81 EmbeddedVector<char, 48> ref_buf; | |
82 WriteData(&dynabuf, &ref_buf); | |
83 CHECK_EQ(48, dynabuf.Write(ref_buf.start(), ref_buf.length())); | |
84 // Verify that we can't write data when remaining buffer space isn't enough. | |
85 CHECK_EQ(0, dynabuf.Write(ref_buf.start(), ref_buf.length())); | |
86 EmbeddedVector<char, 48> buf; | |
87 CHECK_EQ(48, ReadData(&dynabuf, 0, &buf)); | |
88 CHECK_EQ(ref_buf, buf); | |
89 CHECK_EQ(48, ReadData(&dynabuf, 48, &buf)); | |
90 CHECK_EQ(ref_buf, buf); | |
91 CHECK_EQ(0, ReadData(&dynabuf, 48 * 2, &buf)); | |
92 } | |
93 | |
94 | |
95 TEST(DynaBufReadTruncation) { | |
96 LogDynamicBuffer dynabuf(32, 128); | |
97 EmbeddedVector<char, 128> ref_buf; | |
98 WriteData(&dynabuf, &ref_buf); | |
99 EmbeddedVector<char, 128> buf; | |
100 CHECK_EQ(128, ReadData(&dynabuf, 0, &buf)); | |
101 CHECK_EQ(ref_buf, buf); | |
102 // Try to read near the end with a buffer larger than remaining data size. | |
103 EmbeddedVector<char, 48> tail_buf; | |
104 CHECK_EQ(32, ReadData(&dynabuf, 128 - 32, &tail_buf)); | |
105 CHECK_EQ(ref_buf.SubVector(128 - 32, 128), tail_buf.SubVector(0, 32)); | |
106 } | |
107 | |
108 #endif // ENABLE_LOGGING_AND_PROFILING | |
OLD | NEW |