Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: base/big_endian_unittest.cc

Issue 1538743002: Switch to standard integer types in base/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: DEPS roll too Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/big_endian.cc ('k') | base/bind_helpers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "base/big_endian.h" 5 #include "base/big_endian.h"
6 6
7 #include <stdint.h>
8
7 #include "base/strings/string_piece.h" 9 #include "base/strings/string_piece.h"
8 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
9 11
10 namespace base { 12 namespace base {
11 13
12 TEST(BigEndianReaderTest, ReadsValues) { 14 TEST(BigEndianReaderTest, ReadsValues) {
13 char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 15 char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
14 0x1A, 0x2B, 0x3C, 0x4D, 0x5E }; 16 0x1A, 0x2B, 0x3C, 0x4D, 0x5E };
15 char buf[2]; 17 char buf[2];
16 uint8 u8; 18 uint8_t u8;
17 uint16 u16; 19 uint16_t u16;
18 uint32 u32; 20 uint32_t u32;
19 uint64 u64; 21 uint64_t u64;
20 base::StringPiece piece; 22 base::StringPiece piece;
21 BigEndianReader reader(data, sizeof(data)); 23 BigEndianReader reader(data, sizeof(data));
22 24
23 EXPECT_TRUE(reader.Skip(2)); 25 EXPECT_TRUE(reader.Skip(2));
24 EXPECT_EQ(data + 2, reader.ptr()); 26 EXPECT_EQ(data + 2, reader.ptr());
25 EXPECT_EQ(reader.remaining(), static_cast<int>(sizeof(data)) - 2); 27 EXPECT_EQ(reader.remaining(), static_cast<int>(sizeof(data)) - 2);
26 EXPECT_TRUE(reader.ReadBytes(buf, sizeof(buf))); 28 EXPECT_TRUE(reader.ReadBytes(buf, sizeof(buf)));
27 EXPECT_EQ(0x2, buf[0]); 29 EXPECT_EQ(0x2, buf[0]);
28 EXPECT_EQ(0x3, buf[1]); 30 EXPECT_EQ(0x3, buf[1]);
29 EXPECT_TRUE(reader.ReadU8(&u8)); 31 EXPECT_TRUE(reader.ReadU8(&u8));
30 EXPECT_EQ(0x4, u8); 32 EXPECT_EQ(0x4, u8);
31 EXPECT_TRUE(reader.ReadU16(&u16)); 33 EXPECT_TRUE(reader.ReadU16(&u16));
32 EXPECT_EQ(0x0506, u16); 34 EXPECT_EQ(0x0506, u16);
33 EXPECT_TRUE(reader.ReadU32(&u32)); 35 EXPECT_TRUE(reader.ReadU32(&u32));
34 EXPECT_EQ(0x0708090Au, u32); 36 EXPECT_EQ(0x0708090Au, u32);
35 EXPECT_TRUE(reader.ReadU64(&u64)); 37 EXPECT_TRUE(reader.ReadU64(&u64));
36 EXPECT_EQ(0x0B0C0D0E0F1A2B3Cllu, u64); 38 EXPECT_EQ(0x0B0C0D0E0F1A2B3Cllu, u64);
37 base::StringPiece expected(reader.ptr(), 2); 39 base::StringPiece expected(reader.ptr(), 2);
38 EXPECT_TRUE(reader.ReadPiece(&piece, 2)); 40 EXPECT_TRUE(reader.ReadPiece(&piece, 2));
39 EXPECT_EQ(2u, piece.size()); 41 EXPECT_EQ(2u, piece.size());
40 EXPECT_EQ(expected.data(), piece.data()); 42 EXPECT_EQ(expected.data(), piece.data());
41 } 43 }
42 44
43 TEST(BigEndianReaderTest, RespectsLength) { 45 TEST(BigEndianReaderTest, RespectsLength) {
44 char data[8]; 46 char data[8];
45 char buf[2]; 47 char buf[2];
46 uint8 u8; 48 uint8_t u8;
47 uint16 u16; 49 uint16_t u16;
48 uint32 u32; 50 uint32_t u32;
49 uint64 u64; 51 uint64_t u64;
50 base::StringPiece piece; 52 base::StringPiece piece;
51 BigEndianReader reader(data, sizeof(data)); 53 BigEndianReader reader(data, sizeof(data));
52 // 8 left 54 // 8 left
53 EXPECT_FALSE(reader.Skip(9)); 55 EXPECT_FALSE(reader.Skip(9));
54 EXPECT_TRUE(reader.Skip(1)); 56 EXPECT_TRUE(reader.Skip(1));
55 // 7 left 57 // 7 left
56 EXPECT_FALSE(reader.ReadU64(&u64)); 58 EXPECT_FALSE(reader.ReadU64(&u64));
57 EXPECT_TRUE(reader.Skip(4)); 59 EXPECT_TRUE(reader.Skip(4));
58 // 3 left 60 // 3 left
59 EXPECT_FALSE(reader.ReadU32(&u32)); 61 EXPECT_FALSE(reader.ReadU32(&u32));
(...skipping 21 matching lines...) Expand all
81 EXPECT_TRUE(writer.WriteU8(0x4)); 83 EXPECT_TRUE(writer.WriteU8(0x4));
82 EXPECT_TRUE(writer.WriteU16(0x0506)); 84 EXPECT_TRUE(writer.WriteU16(0x0506));
83 EXPECT_TRUE(writer.WriteU32(0x0708090A)); 85 EXPECT_TRUE(writer.WriteU32(0x0708090A));
84 EXPECT_TRUE(writer.WriteU64(0x0B0C0D0E0F1A2B3Cllu)); 86 EXPECT_TRUE(writer.WriteU64(0x0B0C0D0E0F1A2B3Cllu));
85 EXPECT_EQ(0, memcmp(expected, data, sizeof(expected))); 87 EXPECT_EQ(0, memcmp(expected, data, sizeof(expected)));
86 } 88 }
87 89
88 TEST(BigEndianWriterTest, RespectsLength) { 90 TEST(BigEndianWriterTest, RespectsLength) {
89 char data[8]; 91 char data[8];
90 char buf[2]; 92 char buf[2];
91 uint8 u8 = 0; 93 uint8_t u8 = 0;
92 uint16 u16 = 0; 94 uint16_t u16 = 0;
93 uint32 u32 = 0; 95 uint32_t u32 = 0;
94 uint64 u64 = 0; 96 uint64_t u64 = 0;
95 BigEndianWriter writer(data, sizeof(data)); 97 BigEndianWriter writer(data, sizeof(data));
96 // 8 left 98 // 8 left
97 EXPECT_FALSE(writer.Skip(9)); 99 EXPECT_FALSE(writer.Skip(9));
98 EXPECT_TRUE(writer.Skip(1)); 100 EXPECT_TRUE(writer.Skip(1));
99 // 7 left 101 // 7 left
100 EXPECT_FALSE(writer.WriteU64(u64)); 102 EXPECT_FALSE(writer.WriteU64(u64));
101 EXPECT_TRUE(writer.Skip(4)); 103 EXPECT_TRUE(writer.Skip(4));
102 // 3 left 104 // 3 left
103 EXPECT_FALSE(writer.WriteU32(u32)); 105 EXPECT_FALSE(writer.WriteU32(u32));
104 EXPECT_TRUE(writer.Skip(2)); 106 EXPECT_TRUE(writer.Skip(2));
105 // 1 left 107 // 1 left
106 EXPECT_FALSE(writer.WriteU16(u16)); 108 EXPECT_FALSE(writer.WriteU16(u16));
107 EXPECT_FALSE(writer.WriteBytes(buf, 2)); 109 EXPECT_FALSE(writer.WriteBytes(buf, 2));
108 EXPECT_TRUE(writer.Skip(1)); 110 EXPECT_TRUE(writer.Skip(1));
109 // 0 left 111 // 0 left
110 EXPECT_FALSE(writer.WriteU8(u8)); 112 EXPECT_FALSE(writer.WriteU8(u8));
111 EXPECT_EQ(0, writer.remaining()); 113 EXPECT_EQ(0, writer.remaining());
112 } 114 }
113 115
114 } // namespace base 116 } // namespace base
OLDNEW
« no previous file with comments | « base/big_endian.cc ('k') | base/bind_helpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698