sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 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 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 12 | using v8::internal::CStrVector; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 13 | using v8::internal::EmbeddedVector; |
| 14 | using v8::internal::LogDynamicBuffer; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 15 | using v8::internal::MutableCStrVector; |
| 16 | using v8::internal::ScopedVector; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 17 | using v8::internal::Vector; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 18 | using v8::internal::StrLength; |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 19 | |
| 20 | // Fills 'ref_buffer' with test data: a sequence of two-digit |
| 21 | // hex numbers: '0001020304...'. Then writes 'ref_buffer' contents to 'dynabuf'. |
| 22 | static void WriteData(LogDynamicBuffer* dynabuf, Vector<char>* ref_buffer) { |
| 23 | static const char kHex[] = "0123456789ABCDEF"; |
| 24 | CHECK_GT(ref_buffer->length(), 0); |
| 25 | CHECK_GT(513, ref_buffer->length()); |
| 26 | for (int i = 0, half_len = ref_buffer->length() >> 1; i < half_len; ++i) { |
| 27 | (*ref_buffer)[i << 1] = kHex[i >> 4]; |
| 28 | (*ref_buffer)[(i << 1) + 1] = kHex[i & 15]; |
| 29 | } |
| 30 | if (ref_buffer->length() & 1) { |
| 31 | ref_buffer->last() = kHex[ref_buffer->length() >> 5]; |
| 32 | } |
| 33 | CHECK_EQ(ref_buffer->length(), |
| 34 | dynabuf->Write(ref_buffer->start(), ref_buffer->length())); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | static int ReadData( |
| 39 | LogDynamicBuffer* dynabuf, int start_pos, i::Vector<char>* buffer) { |
| 40 | return dynabuf->Read(start_pos, buffer->start(), buffer->length()); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | // Helper function used by CHECK_EQ to compare Vectors. Templatized to |
| 45 | // accept both "char" and "const char" vector contents. |
| 46 | template <typename E, typename V> |
| 47 | static inline void CheckEqualsHelper(const char* file, int line, |
| 48 | const char* expected_source, |
| 49 | const Vector<E>& expected, |
| 50 | const char* value_source, |
| 51 | const Vector<V>& value) { |
| 52 | if (expected.length() != value.length()) { |
| 53 | V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n" |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 54 | "# Vectors lengths differ: %d expected, %d found\n" |
| 55 | "# Expected: %.*s\n" |
| 56 | "# Found: %.*s", |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 57 | expected_source, value_source, |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 58 | expected.length(), value.length(), |
| 59 | expected.length(), expected.start(), |
| 60 | value.length(), value.start()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 61 | } |
| 62 | if (strncmp(expected.start(), value.start(), expected.length()) != 0) { |
| 63 | V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n" |
| 64 | "# Vectors contents differ:\n" |
| 65 | "# Expected: %.*s\n" |
| 66 | "# Found: %.*s", |
| 67 | expected_source, value_source, |
| 68 | expected.length(), expected.start(), |
| 69 | value.length(), value.start()); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | TEST(DynaBufSingleBlock) { |
| 75 | LogDynamicBuffer dynabuf(32, 32, "", 0); |
| 76 | EmbeddedVector<char, 32> ref_buf; |
| 77 | WriteData(&dynabuf, &ref_buf); |
| 78 | EmbeddedVector<char, 32> buf; |
| 79 | CHECK_EQ(32, dynabuf.Read(0, buf.start(), buf.length())); |
| 80 | CHECK_EQ(32, ReadData(&dynabuf, 0, &buf)); |
| 81 | CHECK_EQ(ref_buf, buf); |
| 82 | |
| 83 | // Verify that we can't read and write past the end. |
| 84 | CHECK_EQ(0, dynabuf.Read(32, buf.start(), buf.length())); |
| 85 | CHECK_EQ(0, dynabuf.Write(buf.start(), buf.length())); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | TEST(DynaBufCrossBlocks) { |
| 90 | LogDynamicBuffer dynabuf(32, 128, "", 0); |
| 91 | EmbeddedVector<char, 48> ref_buf; |
| 92 | WriteData(&dynabuf, &ref_buf); |
| 93 | CHECK_EQ(48, dynabuf.Write(ref_buf.start(), ref_buf.length())); |
| 94 | // Verify that we can't write data when remaining buffer space isn't enough. |
| 95 | CHECK_EQ(0, dynabuf.Write(ref_buf.start(), ref_buf.length())); |
| 96 | EmbeddedVector<char, 48> buf; |
| 97 | CHECK_EQ(48, ReadData(&dynabuf, 0, &buf)); |
| 98 | CHECK_EQ(ref_buf, buf); |
| 99 | CHECK_EQ(48, ReadData(&dynabuf, 48, &buf)); |
| 100 | CHECK_EQ(ref_buf, buf); |
| 101 | CHECK_EQ(0, ReadData(&dynabuf, 48 * 2, &buf)); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | TEST(DynaBufReadTruncation) { |
| 106 | LogDynamicBuffer dynabuf(32, 128, "", 0); |
| 107 | EmbeddedVector<char, 128> ref_buf; |
| 108 | WriteData(&dynabuf, &ref_buf); |
| 109 | EmbeddedVector<char, 128> buf; |
| 110 | CHECK_EQ(128, ReadData(&dynabuf, 0, &buf)); |
| 111 | CHECK_EQ(ref_buf, buf); |
| 112 | // Try to read near the end with a buffer larger than remaining data size. |
| 113 | EmbeddedVector<char, 48> tail_buf; |
| 114 | CHECK_EQ(32, ReadData(&dynabuf, 128 - 32, &tail_buf)); |
| 115 | CHECK_EQ(ref_buf.SubVector(128 - 32, 128), tail_buf.SubVector(0, 32)); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | TEST(DynaBufSealing) { |
| 120 | const char* seal = "Sealed"; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 121 | const int seal_size = StrLength(seal); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 122 | LogDynamicBuffer dynabuf(32, 128, seal, seal_size); |
| 123 | EmbeddedVector<char, 100> ref_buf; |
| 124 | WriteData(&dynabuf, &ref_buf); |
| 125 | // Try to write data that will not fit in the buffer. |
| 126 | CHECK_EQ(0, dynabuf.Write(ref_buf.start(), 128 - 100 - seal_size + 1)); |
| 127 | // Now the buffer is sealed, writing of any amount of data is forbidden. |
| 128 | CHECK_EQ(0, dynabuf.Write(ref_buf.start(), 1)); |
| 129 | EmbeddedVector<char, 100> buf; |
| 130 | CHECK_EQ(100, ReadData(&dynabuf, 0, &buf)); |
| 131 | CHECK_EQ(ref_buf, buf); |
| 132 | // Check the seal. |
| 133 | EmbeddedVector<char, 50> seal_buf; |
| 134 | CHECK_EQ(seal_size, ReadData(&dynabuf, 100, &seal_buf)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 135 | CHECK_EQ(CStrVector(seal), seal_buf.SubVector(0, seal_size)); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 136 | // Verify that there's no data beyond the seal. |
| 137 | CHECK_EQ(0, ReadData(&dynabuf, 100 + seal_size, &buf)); |
| 138 | } |
| 139 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 140 | #endif // ENABLE_LOGGING_AND_PROFILING |