| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
| 5 #include <vector> | 8 #include <vector> |
| 6 | 9 |
| 7 #include "chromeos/binder/transaction_data_reader.h" | 10 #include "chromeos/binder/transaction_data_reader.h" |
| 8 #include "chromeos/binder/writable_transaction_data.h" | 11 #include "chromeos/binder/writable_transaction_data.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 namespace binder { | 14 namespace binder { |
| 12 | 15 |
| 13 TEST(BinderTransactionDataReaderTest, ReadData) { | 16 TEST(BinderTransactionDataReaderTest, ReadData) { |
| 14 std::vector<char> input(3); | 17 std::vector<char> input(3); |
| 15 for (size_t i = 0; i < input.size(); ++i) { | 18 for (size_t i = 0; i < input.size(); ++i) { |
| 16 input[i] = i; | 19 input[i] = i; |
| 17 } | 20 } |
| 18 WritableTransactionData data; | 21 WritableTransactionData data; |
| 19 data.WriteData(input.data(), input.size()); | 22 data.WriteData(input.data(), input.size()); |
| 20 EXPECT_EQ(4u, data.GetDataSize()); // Padded for 4-byte alignment. | 23 EXPECT_EQ(4u, data.GetDataSize()); // Padded for 4-byte alignment. |
| 21 | 24 |
| 22 TransactionDataReader reader(data); | 25 TransactionDataReader reader(data); |
| 23 EXPECT_TRUE(reader.HasMoreData()); | 26 EXPECT_TRUE(reader.HasMoreData()); |
| 24 | 27 |
| 25 std::vector<char> result(input.size()); | 28 std::vector<char> result(input.size()); |
| 26 EXPECT_TRUE(reader.ReadData(result.data(), result.size())); | 29 EXPECT_TRUE(reader.ReadData(result.data(), result.size())); |
| 27 EXPECT_EQ(input, result); | 30 EXPECT_EQ(input, result); |
| 28 // Although we read only 3 bytes, we've already consumed 4 bytes because of | 31 // Although we read only 3 bytes, we've already consumed 4 bytes because of |
| 29 // padding. | 32 // padding. |
| 30 EXPECT_FALSE(reader.HasMoreData()); | 33 EXPECT_FALSE(reader.HasMoreData()); |
| 31 } | 34 } |
| 32 | 35 |
| 33 TEST(BinderTransactionDataReaderTest, ReadScalarValues) { | 36 TEST(BinderTransactionDataReaderTest, ReadScalarValues) { |
| 34 const int32 kInt32Value = -1; | 37 const int32_t kInt32Value = -1; |
| 35 const uint32 kUint32Value = 2; | 38 const uint32_t kUint32Value = 2; |
| 36 const int64 kInt64Value = -3; | 39 const int64_t kInt64Value = -3; |
| 37 const uint64 kUint64Value = 4; | 40 const uint64_t kUint64Value = 4; |
| 38 const float kFloatValue = 5.55; | 41 const float kFloatValue = 5.55; |
| 39 const double kDoubleValue = 6.66; | 42 const double kDoubleValue = 6.66; |
| 40 | 43 |
| 41 WritableTransactionData data; | 44 WritableTransactionData data; |
| 42 data.WriteInt32(kInt32Value); | 45 data.WriteInt32(kInt32Value); |
| 43 data.WriteUint32(kUint32Value); | 46 data.WriteUint32(kUint32Value); |
| 44 data.WriteInt64(kInt64Value); | 47 data.WriteInt64(kInt64Value); |
| 45 data.WriteUint64(kUint64Value); | 48 data.WriteUint64(kUint64Value); |
| 46 data.WriteFloat(kFloatValue); | 49 data.WriteFloat(kFloatValue); |
| 47 data.WriteDouble(kDoubleValue); | 50 data.WriteDouble(kDoubleValue); |
| 48 | 51 |
| 49 TransactionDataReader reader(data); | 52 TransactionDataReader reader(data); |
| 50 EXPECT_TRUE(reader.HasMoreData()); | 53 EXPECT_TRUE(reader.HasMoreData()); |
| 51 { | 54 { |
| 52 int32 result = 0; | 55 int32_t result = 0; |
| 53 EXPECT_TRUE(reader.ReadInt32(&result)); | 56 EXPECT_TRUE(reader.ReadInt32(&result)); |
| 54 EXPECT_EQ(kInt32Value, result); | 57 EXPECT_EQ(kInt32Value, result); |
| 55 } | 58 } |
| 56 { | 59 { |
| 57 uint32 result = 0; | 60 uint32_t result = 0; |
| 58 EXPECT_TRUE(reader.ReadUint32(&result)); | 61 EXPECT_TRUE(reader.ReadUint32(&result)); |
| 59 EXPECT_EQ(kUint32Value, result); | 62 EXPECT_EQ(kUint32Value, result); |
| 60 } | 63 } |
| 61 { | 64 { |
| 62 int64 result = 0; | 65 int64_t result = 0; |
| 63 EXPECT_TRUE(reader.ReadInt64(&result)); | 66 EXPECT_TRUE(reader.ReadInt64(&result)); |
| 64 EXPECT_EQ(kInt64Value, result); | 67 EXPECT_EQ(kInt64Value, result); |
| 65 } | 68 } |
| 66 { | 69 { |
| 67 uint64 result = 0; | 70 uint64_t result = 0; |
| 68 EXPECT_TRUE(reader.ReadUint64(&result)); | 71 EXPECT_TRUE(reader.ReadUint64(&result)); |
| 69 EXPECT_EQ(kUint64Value, result); | 72 EXPECT_EQ(kUint64Value, result); |
| 70 } | 73 } |
| 71 { | 74 { |
| 72 float result = 0; | 75 float result = 0; |
| 73 EXPECT_TRUE(reader.ReadFloat(&result)); | 76 EXPECT_TRUE(reader.ReadFloat(&result)); |
| 74 EXPECT_EQ(kFloatValue, result); | 77 EXPECT_EQ(kFloatValue, result); |
| 75 } | 78 } |
| 76 { | 79 { |
| 77 double result = 0; | 80 double result = 0; |
| 78 EXPECT_TRUE(reader.ReadDouble(&result)); | 81 EXPECT_TRUE(reader.ReadDouble(&result)); |
| 79 EXPECT_EQ(kDoubleValue, result); | 82 EXPECT_EQ(kDoubleValue, result); |
| 80 } | 83 } |
| 81 EXPECT_FALSE(reader.HasMoreData()); | 84 EXPECT_FALSE(reader.HasMoreData()); |
| 82 } | 85 } |
| 83 | 86 |
| 84 } // namespace binder | 87 } // namespace binder |
| OLD | NEW |