pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/transient/file_utils.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 14 | #include <memory> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #include <string> |
jbauch | 541f186 | 2016-02-10 09:09:54 -0800 | [diff] [blame] | 16 | #include <vector> |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 17 | |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 18 | #include "rtc_base/system/file_wrapper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "test/testsupport/file_utils.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | static const uint8_t kPiBytesf[4] = {0xDB, 0x0F, 0x49, 0x40}; |
| 25 | static const uint8_t kEBytesf[4] = {0x54, 0xF8, 0x2D, 0x40}; |
| 26 | static const uint8_t kAvogadroBytesf[4] = {0x2F, 0x0C, 0xFF, 0x66}; |
| 27 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 28 | static const uint8_t kPiBytes[8] = {0x18, 0x2D, 0x44, 0x54, |
| 29 | 0xFB, 0x21, 0x09, 0x40}; |
| 30 | static const uint8_t kEBytes[8] = {0x69, 0x57, 0x14, 0x8B, |
| 31 | 0x0A, 0xBF, 0x05, 0x40}; |
| 32 | static const uint8_t kAvogadroBytes[8] = {0xF4, 0xBC, 0xA8, 0xDF, |
| 33 | 0x85, 0xE1, 0xDF, 0x44}; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 34 | |
| 35 | static const double kPi = 3.14159265358979323846; |
| 36 | static const double kE = 2.71828182845904523536; |
| 37 | static const double kAvogadro = 602214100000000000000000.0; |
| 38 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | class TransientFileUtilsTest : public ::testing::Test { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 40 | protected: |
| 41 | TransientFileUtilsTest() |
| 42 | : kTestFileName( |
| 43 | test::ResourcePath("audio_processing/transient/double-utils", |
| 44 | "dat")), |
| 45 | kTestFileNamef( |
| 46 | test::ResourcePath("audio_processing/transient/float-utils", |
| 47 | "dat")) {} |
jbauch | 541f186 | 2016-02-10 09:09:54 -0800 | [diff] [blame] | 48 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | ~TransientFileUtilsTest() override { CleanupTempFiles(); } |
jbauch | 541f186 | 2016-02-10 09:09:54 -0800 | [diff] [blame] | 50 | |
| 51 | std::string CreateTempFilename(const std::string& dir, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | const std::string& prefix) { |
jbauch | 541f186 | 2016-02-10 09:09:54 -0800 | [diff] [blame] | 53 | std::string filename = test::TempFilename(dir, prefix); |
| 54 | temp_filenames_.push_back(filename); |
| 55 | return filename; |
| 56 | } |
| 57 | |
| 58 | void CleanupTempFiles() { |
| 59 | for (const std::string& filename : temp_filenames_) { |
| 60 | remove(filename.c_str()); |
| 61 | } |
| 62 | temp_filenames_.clear(); |
| 63 | } |
| 64 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 65 | // This file (used in some tests) contains binary data. The data correspond to |
| 66 | // the double representation of the constants: Pi, E, and the Avogadro's |
| 67 | // Number; |
| 68 | // appended in that order. |
| 69 | const std::string kTestFileName; |
| 70 | |
| 71 | // This file (used in some tests) contains binary data. The data correspond to |
| 72 | // the float representation of the constants: Pi, E, and the Avogadro's |
| 73 | // Number; |
| 74 | // appended in that order. |
| 75 | const std::string kTestFileNamef; |
jbauch | 541f186 | 2016-02-10 09:09:54 -0800 | [diff] [blame] | 76 | |
| 77 | // List of temporary filenames created by CreateTempFilename. |
| 78 | std::vector<std::string> temp_filenames_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 81 | #if defined(WEBRTC_IOS) |
| 82 | #define MAYBE_ConvertByteArrayToFloat DISABLED_ConvertByteArrayToFloat |
| 83 | #else |
| 84 | #define MAYBE_ConvertByteArrayToFloat ConvertByteArrayToFloat |
| 85 | #endif |
| 86 | TEST_F(TransientFileUtilsTest, MAYBE_ConvertByteArrayToFloat) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 87 | float value = 0.0; |
| 88 | |
| 89 | EXPECT_EQ(0, ConvertByteArrayToFloat(kPiBytesf, &value)); |
| 90 | EXPECT_FLOAT_EQ(kPi, value); |
| 91 | |
| 92 | EXPECT_EQ(0, ConvertByteArrayToFloat(kEBytesf, &value)); |
| 93 | EXPECT_FLOAT_EQ(kE, value); |
| 94 | |
| 95 | EXPECT_EQ(0, ConvertByteArrayToFloat(kAvogadroBytesf, &value)); |
| 96 | EXPECT_FLOAT_EQ(kAvogadro, value); |
| 97 | } |
| 98 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 99 | #if defined(WEBRTC_IOS) |
| 100 | #define MAYBE_ConvertByteArrayToDouble DISABLED_ConvertByteArrayToDouble |
| 101 | #else |
| 102 | #define MAYBE_ConvertByteArrayToDouble ConvertByteArrayToDouble |
| 103 | #endif |
| 104 | TEST_F(TransientFileUtilsTest, MAYBE_ConvertByteArrayToDouble) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 105 | double value = 0.0; |
| 106 | |
| 107 | EXPECT_EQ(0, ConvertByteArrayToDouble(kPiBytes, &value)); |
| 108 | EXPECT_DOUBLE_EQ(kPi, value); |
| 109 | |
| 110 | EXPECT_EQ(0, ConvertByteArrayToDouble(kEBytes, &value)); |
| 111 | EXPECT_DOUBLE_EQ(kE, value); |
| 112 | |
| 113 | EXPECT_EQ(0, ConvertByteArrayToDouble(kAvogadroBytes, &value)); |
| 114 | EXPECT_DOUBLE_EQ(kAvogadro, value); |
| 115 | } |
| 116 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 117 | #if defined(WEBRTC_IOS) |
| 118 | #define MAYBE_ConvertFloatToByteArray DISABLED_ConvertFloatToByteArray |
| 119 | #else |
| 120 | #define MAYBE_ConvertFloatToByteArray ConvertFloatToByteArray |
| 121 | #endif |
| 122 | TEST_F(TransientFileUtilsTest, MAYBE_ConvertFloatToByteArray) { |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 123 | std::unique_ptr<uint8_t[]> bytes(new uint8_t[4]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 124 | |
| 125 | EXPECT_EQ(0, ConvertFloatToByteArray(kPi, bytes.get())); |
| 126 | EXPECT_EQ(0, memcmp(bytes.get(), kPiBytesf, 4)); |
| 127 | |
| 128 | EXPECT_EQ(0, ConvertFloatToByteArray(kE, bytes.get())); |
| 129 | EXPECT_EQ(0, memcmp(bytes.get(), kEBytesf, 4)); |
| 130 | |
| 131 | EXPECT_EQ(0, ConvertFloatToByteArray(kAvogadro, bytes.get())); |
| 132 | EXPECT_EQ(0, memcmp(bytes.get(), kAvogadroBytesf, 4)); |
| 133 | } |
| 134 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 135 | #if defined(WEBRTC_IOS) |
| 136 | #define MAYBE_ConvertDoubleToByteArray DISABLED_ConvertDoubleToByteArray |
| 137 | #else |
| 138 | #define MAYBE_ConvertDoubleToByteArray ConvertDoubleToByteArray |
| 139 | #endif |
| 140 | TEST_F(TransientFileUtilsTest, MAYBE_ConvertDoubleToByteArray) { |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 141 | std::unique_ptr<uint8_t[]> bytes(new uint8_t[8]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 142 | |
| 143 | EXPECT_EQ(0, ConvertDoubleToByteArray(kPi, bytes.get())); |
| 144 | EXPECT_EQ(0, memcmp(bytes.get(), kPiBytes, 8)); |
| 145 | |
| 146 | EXPECT_EQ(0, ConvertDoubleToByteArray(kE, bytes.get())); |
| 147 | EXPECT_EQ(0, memcmp(bytes.get(), kEBytes, 8)); |
| 148 | |
| 149 | EXPECT_EQ(0, ConvertDoubleToByteArray(kAvogadro, bytes.get())); |
| 150 | EXPECT_EQ(0, memcmp(bytes.get(), kAvogadroBytes, 8)); |
| 151 | } |
| 152 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 153 | #if defined(WEBRTC_IOS) |
| 154 | #define MAYBE_ReadInt16BufferFromFile DISABLED_ReadInt16BufferFromFile |
| 155 | #else |
| 156 | #define MAYBE_ReadInt16BufferFromFile ReadInt16BufferFromFile |
| 157 | #endif |
| 158 | TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16BufferFromFile) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 159 | std::string test_filename = kTestFileName; |
| 160 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 161 | FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str()); |
| 162 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 163 | << kTestFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 164 | |
| 165 | const size_t kBufferLength = 12; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 166 | std::unique_ptr<int16_t[]> buffer(new int16_t[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 167 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 168 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 169 | ReadInt16BufferFromFile(&file, kBufferLength, buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 170 | EXPECT_EQ(22377, buffer[4]); |
| 171 | EXPECT_EQ(16389, buffer[7]); |
| 172 | EXPECT_EQ(17631, buffer[kBufferLength - 1]); |
| 173 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 174 | file.Rewind(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 175 | |
| 176 | // The next test is for checking the case where there are not as much data as |
| 177 | // needed in the file, but reads to the end, and it returns the number of |
| 178 | // int16s read. |
| 179 | const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; |
| 180 | buffer.reset(new int16_t[kBufferLenghtLargerThanFile]); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 181 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 182 | ReadInt16BufferFromFile(&file, kBufferLenghtLargerThanFile, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 183 | buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 184 | EXPECT_EQ(11544, buffer[0]); |
| 185 | EXPECT_EQ(22377, buffer[4]); |
| 186 | EXPECT_EQ(16389, buffer[7]); |
| 187 | EXPECT_EQ(17631, buffer[kBufferLength - 1]); |
| 188 | } |
| 189 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 190 | #if defined(WEBRTC_IOS) |
| 191 | #define MAYBE_ReadInt16FromFileToFloatBuffer \ |
| 192 | DISABLED_ReadInt16FromFileToFloatBuffer |
| 193 | #else |
| 194 | #define MAYBE_ReadInt16FromFileToFloatBuffer ReadInt16FromFileToFloatBuffer |
| 195 | #endif |
| 196 | TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToFloatBuffer) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 197 | std::string test_filename = kTestFileName; |
| 198 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 199 | FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str()); |
| 200 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 201 | << kTestFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 202 | |
| 203 | const size_t kBufferLength = 12; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 204 | std::unique_ptr<float[]> buffer(new float[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 205 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 206 | EXPECT_EQ(kBufferLength, |
| 207 | ReadInt16FromFileToFloatBuffer(&file, kBufferLength, buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 208 | |
| 209 | EXPECT_DOUBLE_EQ(11544, buffer[0]); |
| 210 | EXPECT_DOUBLE_EQ(22377, buffer[4]); |
| 211 | EXPECT_DOUBLE_EQ(16389, buffer[7]); |
| 212 | EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]); |
| 213 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 214 | file.Rewind(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 215 | |
| 216 | // The next test is for checking the case where there are not as much data as |
| 217 | // needed in the file, but reads to the end, and it returns the number of |
| 218 | // int16s read. |
| 219 | const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; |
| 220 | buffer.reset(new float[kBufferLenghtLargerThanFile]); |
| 221 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 222 | ReadInt16FromFileToFloatBuffer(&file, kBufferLenghtLargerThanFile, |
| 223 | buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 224 | EXPECT_DOUBLE_EQ(11544, buffer[0]); |
| 225 | EXPECT_DOUBLE_EQ(22377, buffer[4]); |
| 226 | EXPECT_DOUBLE_EQ(16389, buffer[7]); |
| 227 | EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]); |
| 228 | } |
| 229 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 230 | #if defined(WEBRTC_IOS) |
| 231 | #define MAYBE_ReadInt16FromFileToDoubleBuffer \ |
| 232 | DISABLED_ReadInt16FromFileToDoubleBuffer |
| 233 | #else |
| 234 | #define MAYBE_ReadInt16FromFileToDoubleBuffer ReadInt16FromFileToDoubleBuffer |
| 235 | #endif |
| 236 | TEST_F(TransientFileUtilsTest, MAYBE_ReadInt16FromFileToDoubleBuffer) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 237 | std::string test_filename = kTestFileName; |
| 238 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 239 | FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str()); |
| 240 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 241 | << kTestFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 242 | |
| 243 | const size_t kBufferLength = 12; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 244 | std::unique_ptr<double[]> buffer(new double[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 245 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 246 | EXPECT_EQ(kBufferLength, ReadInt16FromFileToDoubleBuffer(&file, kBufferLength, |
| 247 | buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 248 | EXPECT_DOUBLE_EQ(11544, buffer[0]); |
| 249 | EXPECT_DOUBLE_EQ(22377, buffer[4]); |
| 250 | EXPECT_DOUBLE_EQ(16389, buffer[7]); |
| 251 | EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]); |
| 252 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 253 | file.Rewind(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 254 | |
| 255 | // The next test is for checking the case where there are not as much data as |
| 256 | // needed in the file, but reads to the end, and it returns the number of |
| 257 | // int16s read. |
| 258 | const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; |
| 259 | buffer.reset(new double[kBufferLenghtLargerThanFile]); |
| 260 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 261 | ReadInt16FromFileToDoubleBuffer(&file, kBufferLenghtLargerThanFile, |
| 262 | buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 263 | EXPECT_DOUBLE_EQ(11544, buffer[0]); |
| 264 | EXPECT_DOUBLE_EQ(22377, buffer[4]); |
| 265 | EXPECT_DOUBLE_EQ(16389, buffer[7]); |
| 266 | EXPECT_DOUBLE_EQ(17631, buffer[kBufferLength - 1]); |
| 267 | } |
| 268 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 269 | #if defined(WEBRTC_IOS) |
| 270 | #define MAYBE_ReadFloatBufferFromFile DISABLED_ReadFloatBufferFromFile |
| 271 | #else |
| 272 | #define MAYBE_ReadFloatBufferFromFile ReadFloatBufferFromFile |
| 273 | #endif |
| 274 | TEST_F(TransientFileUtilsTest, MAYBE_ReadFloatBufferFromFile) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 275 | std::string test_filename = kTestFileNamef; |
| 276 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 277 | FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str()); |
| 278 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 279 | << kTestFileNamef.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 280 | |
| 281 | const size_t kBufferLength = 3; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 282 | std::unique_ptr<float[]> buffer(new float[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 283 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 284 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 285 | ReadFloatBufferFromFile(&file, kBufferLength, buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 286 | EXPECT_FLOAT_EQ(kPi, buffer[0]); |
| 287 | EXPECT_FLOAT_EQ(kE, buffer[1]); |
| 288 | EXPECT_FLOAT_EQ(kAvogadro, buffer[2]); |
| 289 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 290 | file.Rewind(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 291 | |
| 292 | // The next test is for checking the case where there are not as much data as |
| 293 | // needed in the file, but reads to the end, and it returns the number of |
| 294 | // doubles read. |
| 295 | const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; |
| 296 | buffer.reset(new float[kBufferLenghtLargerThanFile]); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 297 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 298 | ReadFloatBufferFromFile(&file, kBufferLenghtLargerThanFile, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 299 | buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 300 | EXPECT_FLOAT_EQ(kPi, buffer[0]); |
| 301 | EXPECT_FLOAT_EQ(kE, buffer[1]); |
| 302 | EXPECT_FLOAT_EQ(kAvogadro, buffer[2]); |
| 303 | } |
| 304 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 305 | #if defined(WEBRTC_IOS) |
| 306 | #define MAYBE_ReadDoubleBufferFromFile DISABLED_ReadDoubleBufferFromFile |
| 307 | #else |
| 308 | #define MAYBE_ReadDoubleBufferFromFile ReadDoubleBufferFromFile |
| 309 | #endif |
| 310 | TEST_F(TransientFileUtilsTest, MAYBE_ReadDoubleBufferFromFile) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 311 | std::string test_filename = kTestFileName; |
| 312 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 313 | FileWrapper file = FileWrapper::OpenReadOnly(test_filename.c_str()); |
| 314 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 315 | << kTestFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 316 | |
| 317 | const size_t kBufferLength = 3; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 318 | std::unique_ptr<double[]> buffer(new double[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 319 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 320 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 321 | ReadDoubleBufferFromFile(&file, kBufferLength, buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 322 | EXPECT_DOUBLE_EQ(kPi, buffer[0]); |
| 323 | EXPECT_DOUBLE_EQ(kE, buffer[1]); |
| 324 | EXPECT_DOUBLE_EQ(kAvogadro, buffer[2]); |
| 325 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 326 | file.Rewind(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 327 | |
| 328 | // The next test is for checking the case where there are not as much data as |
| 329 | // needed in the file, but reads to the end, and it returns the number of |
| 330 | // doubles read. |
| 331 | const size_t kBufferLenghtLargerThanFile = kBufferLength * 2; |
| 332 | buffer.reset(new double[kBufferLenghtLargerThanFile]); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 333 | EXPECT_EQ(kBufferLength, |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 334 | ReadDoubleBufferFromFile(&file, kBufferLenghtLargerThanFile, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 335 | buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 336 | EXPECT_DOUBLE_EQ(kPi, buffer[0]); |
| 337 | EXPECT_DOUBLE_EQ(kE, buffer[1]); |
| 338 | EXPECT_DOUBLE_EQ(kAvogadro, buffer[2]); |
| 339 | } |
| 340 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 341 | #if defined(WEBRTC_IOS) |
| 342 | #define MAYBE_WriteInt16BufferToFile DISABLED_WriteInt16BufferToFile |
| 343 | #else |
| 344 | #define MAYBE_WriteInt16BufferToFile WriteInt16BufferToFile |
| 345 | #endif |
| 346 | TEST_F(TransientFileUtilsTest, MAYBE_WriteInt16BufferToFile) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 347 | std::string kOutFileName = |
| 348 | CreateTempFilename(test::OutputPath(), "utils_test"); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 349 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 350 | FileWrapper file = FileWrapper::OpenWriteOnly(kOutFileName.c_str()); |
| 351 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 352 | << kOutFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 353 | |
| 354 | const size_t kBufferLength = 3; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 355 | std::unique_ptr<int16_t[]> written_buffer(new int16_t[kBufferLength]); |
| 356 | std::unique_ptr<int16_t[]> read_buffer(new int16_t[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 357 | |
| 358 | written_buffer[0] = 1; |
| 359 | written_buffer[1] = 2; |
| 360 | written_buffer[2] = 3; |
| 361 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 362 | EXPECT_EQ(kBufferLength, |
| 363 | WriteInt16BufferToFile(&file, kBufferLength, written_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 364 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 365 | file.Close(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 366 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 367 | file = FileWrapper::OpenReadOnly(kOutFileName.c_str()); |
| 368 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 369 | << kOutFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 370 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 371 | EXPECT_EQ(kBufferLength, |
| 372 | ReadInt16BufferFromFile(&file, kBufferLength, read_buffer.get())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 373 | EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 374 | kBufferLength * sizeof(written_buffer[0]))); |
| 375 | } |
| 376 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 377 | #if defined(WEBRTC_IOS) |
| 378 | #define MAYBE_WriteFloatBufferToFile DISABLED_WriteFloatBufferToFile |
| 379 | #else |
| 380 | #define MAYBE_WriteFloatBufferToFile WriteFloatBufferToFile |
| 381 | #endif |
| 382 | TEST_F(TransientFileUtilsTest, MAYBE_WriteFloatBufferToFile) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 383 | std::string kOutFileName = |
| 384 | CreateTempFilename(test::OutputPath(), "utils_test"); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 385 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 386 | FileWrapper file = FileWrapper::OpenWriteOnly(kOutFileName.c_str()); |
| 387 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 388 | << kOutFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 389 | |
| 390 | const size_t kBufferLength = 3; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 391 | std::unique_ptr<float[]> written_buffer(new float[kBufferLength]); |
| 392 | std::unique_ptr<float[]> read_buffer(new float[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 393 | |
conceptgenesis | 3f70562 | 2016-01-30 14:40:44 -0800 | [diff] [blame] | 394 | written_buffer[0] = static_cast<float>(kPi); |
| 395 | written_buffer[1] = static_cast<float>(kE); |
| 396 | written_buffer[2] = static_cast<float>(kAvogadro); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 397 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 398 | EXPECT_EQ(kBufferLength, |
| 399 | WriteFloatBufferToFile(&file, kBufferLength, written_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 400 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 401 | file.Close(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 402 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 403 | file = FileWrapper::OpenReadOnly(kOutFileName.c_str()); |
| 404 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 405 | << kOutFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 406 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 407 | EXPECT_EQ(kBufferLength, |
| 408 | ReadFloatBufferFromFile(&file, kBufferLength, read_buffer.get())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 409 | EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 410 | kBufferLength * sizeof(written_buffer[0]))); |
| 411 | } |
| 412 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 413 | #if defined(WEBRTC_IOS) |
| 414 | #define MAYBE_WriteDoubleBufferToFile DISABLED_WriteDoubleBufferToFile |
| 415 | #else |
| 416 | #define MAYBE_WriteDoubleBufferToFile WriteDoubleBufferToFile |
| 417 | #endif |
| 418 | TEST_F(TransientFileUtilsTest, MAYBE_WriteDoubleBufferToFile) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 419 | std::string kOutFileName = |
| 420 | CreateTempFilename(test::OutputPath(), "utils_test"); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 421 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 422 | FileWrapper file = FileWrapper::OpenWriteOnly(kOutFileName.c_str()); |
| 423 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 424 | << kOutFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 425 | |
| 426 | const size_t kBufferLength = 3; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 427 | std::unique_ptr<double[]> written_buffer(new double[kBufferLength]); |
| 428 | std::unique_ptr<double[]> read_buffer(new double[kBufferLength]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 429 | |
| 430 | written_buffer[0] = kPi; |
| 431 | written_buffer[1] = kE; |
| 432 | written_buffer[2] = kAvogadro; |
| 433 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 434 | EXPECT_EQ(kBufferLength, WriteDoubleBufferToFile(&file, kBufferLength, |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 435 | written_buffer.get())); |
| 436 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 437 | file.Close(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 438 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 439 | file = FileWrapper::OpenReadOnly(kOutFileName.c_str()); |
| 440 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 441 | << kOutFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 442 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 443 | EXPECT_EQ(kBufferLength, |
| 444 | ReadDoubleBufferFromFile(&file, kBufferLength, read_buffer.get())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 445 | EXPECT_EQ(0, memcmp(written_buffer.get(), read_buffer.get(), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 446 | kBufferLength * sizeof(written_buffer[0]))); |
| 447 | } |
| 448 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 449 | #if defined(WEBRTC_IOS) |
| 450 | #define MAYBE_ExpectedErrorReturnValues DISABLED_ExpectedErrorReturnValues |
| 451 | #else |
| 452 | #define MAYBE_ExpectedErrorReturnValues ExpectedErrorReturnValues |
| 453 | #endif |
| 454 | TEST_F(TransientFileUtilsTest, MAYBE_ExpectedErrorReturnValues) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 455 | std::string test_filename = kTestFileName; |
| 456 | |
| 457 | double value; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 458 | std::unique_ptr<int16_t[]> int16_buffer(new int16_t[1]); |
| 459 | std::unique_ptr<double[]> double_buffer(new double[1]); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 460 | FileWrapper file; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 461 | |
| 462 | EXPECT_EQ(-1, ConvertByteArrayToDouble(NULL, &value)); |
| 463 | EXPECT_EQ(-1, ConvertByteArrayToDouble(kPiBytes, NULL)); |
| 464 | |
| 465 | EXPECT_EQ(-1, ConvertDoubleToByteArray(kPi, NULL)); |
| 466 | |
| 467 | // Tests with file not opened. |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 468 | EXPECT_EQ(0u, ReadInt16BufferFromFile(&file, 1, int16_buffer.get())); |
| 469 | EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(&file, 1, double_buffer.get())); |
| 470 | EXPECT_EQ(0u, ReadDoubleBufferFromFile(&file, 1, double_buffer.get())); |
| 471 | EXPECT_EQ(0u, WriteInt16BufferToFile(&file, 1, int16_buffer.get())); |
| 472 | EXPECT_EQ(0u, WriteDoubleBufferToFile(&file, 1, double_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 473 | |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 474 | file = FileWrapper::OpenReadOnly(test_filename.c_str()); |
| 475 | ASSERT_TRUE(file.is_open()) << "File could not be opened:\n" |
| 476 | << kTestFileName.c_str(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 477 | |
| 478 | EXPECT_EQ(0u, ReadInt16BufferFromFile(NULL, 1, int16_buffer.get())); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 479 | EXPECT_EQ(0u, ReadInt16BufferFromFile(&file, 1, NULL)); |
| 480 | EXPECT_EQ(0u, ReadInt16BufferFromFile(&file, 0, int16_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 481 | |
| 482 | EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(NULL, 1, double_buffer.get())); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 483 | EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(&file, 1, NULL)); |
| 484 | EXPECT_EQ(0u, ReadInt16FromFileToDoubleBuffer(&file, 0, double_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 485 | |
| 486 | EXPECT_EQ(0u, ReadDoubleBufferFromFile(NULL, 1, double_buffer.get())); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 487 | EXPECT_EQ(0u, ReadDoubleBufferFromFile(&file, 1, NULL)); |
| 488 | EXPECT_EQ(0u, ReadDoubleBufferFromFile(&file, 0, double_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 489 | |
| 490 | EXPECT_EQ(0u, WriteInt16BufferToFile(NULL, 1, int16_buffer.get())); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 491 | EXPECT_EQ(0u, WriteInt16BufferToFile(&file, 1, NULL)); |
| 492 | EXPECT_EQ(0u, WriteInt16BufferToFile(&file, 0, int16_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 493 | |
| 494 | EXPECT_EQ(0u, WriteDoubleBufferToFile(NULL, 1, double_buffer.get())); |
Niels Möller | 5a6ae02 | 2019-01-21 11:59:10 +0100 | [diff] [blame^] | 495 | EXPECT_EQ(0u, WriteDoubleBufferToFile(&file, 1, NULL)); |
| 496 | EXPECT_EQ(0u, WriteDoubleBufferToFile(&file, 0, double_buffer.get())); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | } // namespace webrtc |