| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <algorithm> | 8 #include <algorithm> |
| 6 #include <vector> | 9 #include <vector> |
| 7 | 10 |
| 8 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 9 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 11 #include "chrome/common/media_galleries/pmp_constants.h" | 14 #include "chrome/common/media_galleries/pmp_constants.h" |
| 12 #include "chrome/common/media_galleries/pmp_test_util.h" | 15 #include "chrome/common/media_galleries/pmp_test_util.h" |
| 13 #include "chrome/utility/media_galleries/pmp_column_reader.h" | 16 #include "chrome/utility/media_galleries/pmp_column_reader.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 35 return false; | 38 return false; |
| 36 | 39 |
| 37 base::File file(temp_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 40 base::File file(temp_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 38 if (!file.IsValid()) | 41 if (!file.IsValid()) |
| 39 return false; | 42 return false; |
| 40 | 43 |
| 41 return reader->ReadFile(&file, expected_type); | 44 return reader->ReadFile(&file, expected_type); |
| 42 } | 45 } |
| 43 | 46 |
| 44 // Overridden version of Read method to make test code templatable. | 47 // Overridden version of Read method to make test code templatable. |
| 45 bool DoRead(const PmpColumnReader* reader, uint32 row, std::string* target) { | 48 bool DoRead(const PmpColumnReader* reader, uint32_t row, std::string* target) { |
| 46 return reader->ReadString(row, target); | 49 return reader->ReadString(row, target); |
| 47 } | 50 } |
| 48 | 51 |
| 49 bool DoRead(const PmpColumnReader* reader, uint32 row, uint32* target) { | 52 bool DoRead(const PmpColumnReader* reader, uint32_t row, uint32_t* target) { |
| 50 return reader->ReadUInt32(row, target); | 53 return reader->ReadUInt32(row, target); |
| 51 } | 54 } |
| 52 | 55 |
| 53 bool DoRead(const PmpColumnReader* reader, uint32 row, double* target) { | 56 bool DoRead(const PmpColumnReader* reader, uint32_t row, double* target) { |
| 54 return reader->ReadDouble64(row, target); | 57 return reader->ReadDouble64(row, target); |
| 55 } | 58 } |
| 56 | 59 |
| 57 bool DoRead(const PmpColumnReader* reader, uint32 row, uint8* target) { | 60 bool DoRead(const PmpColumnReader* reader, uint32_t row, uint8_t* target) { |
| 58 return reader->ReadUInt8(row, target); | 61 return reader->ReadUInt8(row, target); |
| 59 } | 62 } |
| 60 | 63 |
| 61 bool DoRead(const PmpColumnReader* reader, uint32 row, uint64* target) { | 64 bool DoRead(const PmpColumnReader* reader, uint32_t row, uint64_t* target) { |
| 62 return reader->ReadUInt64(row, target); | 65 return reader->ReadUInt64(row, target); |
| 63 } | 66 } |
| 64 | 67 |
| 65 // TestValid | 68 // TestValid |
| 66 template<class T> | 69 template<class T> |
| 67 void TestValid(const PmpFieldType field_type, | 70 void TestValid(const PmpFieldType field_type, |
| 68 const std::vector<T>& elems) { | 71 const std::vector<T>& elems) { |
| 69 PmpColumnReader reader; | 72 PmpColumnReader reader; |
| 70 std::vector<char> data = | 73 std::vector<char> data = |
| 71 PmpTestUtil::MakeHeaderAndBody(field_type, elems.size(), elems); | 74 PmpTestUtil::MakeHeaderAndBody(field_type, elems.size(), elems); |
| 72 ASSERT_TRUE(InitColumnReaderFromBytes(&reader, data, field_type)); | 75 ASSERT_TRUE(InitColumnReaderFromBytes(&reader, data, field_type)); |
| 73 EXPECT_EQ(elems.size(), reader.rows_read()); | 76 EXPECT_EQ(elems.size(), reader.rows_read()); |
| 74 | 77 |
| 75 for (uint32 i = 0; i < elems.size() && i < reader.rows_read(); i++) { | 78 for (uint32_t i = 0; i < elems.size() && i < reader.rows_read(); i++) { |
| 76 T target; | 79 T target; |
| 77 EXPECT_TRUE(DoRead(&reader, i, &target)); | 80 EXPECT_TRUE(DoRead(&reader, i, &target)); |
| 78 EXPECT_EQ(elems[i], target); | 81 EXPECT_EQ(elems[i], target); |
| 79 } | 82 } |
| 80 } | 83 } |
| 81 | 84 |
| 82 template<class T> | 85 template<class T> |
| 83 void TestMalformed(const PmpFieldType field_type, | 86 void TestMalformed(const PmpFieldType field_type, |
| 84 const std::vector<T>& elems) { | 87 const std::vector<T>& elems) { |
| 85 PmpColumnReader reader_too_few_declared_rows; | 88 PmpColumnReader reader_too_few_declared_rows; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 109 PmpTestUtil::MakeHeaderAndBody(field_type, elems.size(), elems); | 112 PmpTestUtil::MakeHeaderAndBody(field_type, elems.size(), elems); |
| 110 data_padded.resize(data_padded.size()+10); | 113 data_padded.resize(data_padded.size()+10); |
| 111 EXPECT_FALSE(InitColumnReaderFromBytes(&reader_padded, | 114 EXPECT_FALSE(InitColumnReaderFromBytes(&reader_padded, |
| 112 data_padded, | 115 data_padded, |
| 113 field_type)); | 116 field_type)); |
| 114 } | 117 } |
| 115 | 118 |
| 116 template<class T> | 119 template<class T> |
| 117 void TestPrimitive(const PmpFieldType field_type) { | 120 void TestPrimitive(const PmpFieldType field_type) { |
| 118 // Make an ascending vector of the primitive. | 121 // Make an ascending vector of the primitive. |
| 119 uint32 n = 100; | 122 uint32_t n = 100; |
| 120 std::vector<T> data(n, 0); | 123 std::vector<T> data(n, 0); |
| 121 for (uint32 i = 0; i < n; i++) { | 124 for (uint32_t i = 0; i < n; i++) { |
| 122 data[i] = i*3; | 125 data[i] = i*3; |
| 123 } | 126 } |
| 124 | 127 |
| 125 TestValid<T>(field_type, data); | 128 TestValid<T>(field_type, data); |
| 126 TestMalformed<T>(field_type, data); | 129 TestMalformed<T>(field_type, data); |
| 127 } | 130 } |
| 128 | 131 |
| 129 | 132 |
| 130 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) { | 133 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) { |
| 131 PmpColumnReader reader_good_header; | 134 PmpColumnReader reader_good_header; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 mixed_strings.push_back(""); | 190 mixed_strings.push_back(""); |
| 188 | 191 |
| 189 // Test that a mixed set of strings read correctly. | 192 // Test that a mixed set of strings read correctly. |
| 190 TestValid(PMP_TYPE_STRING, mixed_strings); | 193 TestValid(PMP_TYPE_STRING, mixed_strings); |
| 191 | 194 |
| 192 // Test with the data messed up in a variety of ways. | 195 // Test with the data messed up in a variety of ways. |
| 193 TestMalformed(PMP_TYPE_STRING, mixed_strings); | 196 TestMalformed(PMP_TYPE_STRING, mixed_strings); |
| 194 } | 197 } |
| 195 | 198 |
| 196 TEST(PmpColumnReaderTest, PrimitiveParsing) { | 199 TEST(PmpColumnReaderTest, PrimitiveParsing) { |
| 197 TestPrimitive<uint32>(PMP_TYPE_UINT32); | 200 TestPrimitive<uint32_t>(PMP_TYPE_UINT32); |
| 198 TestPrimitive<double>(PMP_TYPE_DOUBLE64); | 201 TestPrimitive<double>(PMP_TYPE_DOUBLE64); |
| 199 TestPrimitive<uint8>(PMP_TYPE_UINT8); | 202 TestPrimitive<uint8_t>(PMP_TYPE_UINT8); |
| 200 TestPrimitive<uint64>(PMP_TYPE_UINT64); | 203 TestPrimitive<uint64_t>(PMP_TYPE_UINT64); |
| 201 } | 204 } |
| 202 | 205 |
| 203 } // namespace | 206 } // namespace |
| 204 | 207 |
| 205 } // namespace picasa | 208 } // namespace picasa |
| OLD | NEW |