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