blob: c724e378f52ef909a19ce160a1df5399c1fc3c0d [file] [log] [blame]
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +00001/*
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
11#include "testing/gtest/include/gtest/gtest.h"
12#include "webrtc/common_audio/include/audio_util.h"
13#include "webrtc/typedefs.h"
14
15namespace webrtc {
16
17void ExpectArraysEq(const int16_t* ref, const int16_t* test, int length) {
18 for (int i = 0; i < length; ++i) {
19 EXPECT_EQ(test[i], ref[i]);
20 }
21}
22
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000023TEST(AudioUtilTest, Clamp) {
24 EXPECT_EQ(1000.f, ClampInt16(1000.f));
25 EXPECT_EQ(32767.f, ClampInt16(32767.5f));
26 EXPECT_EQ(-32768.f, ClampInt16(-32768.5f));
27}
28
29TEST(AudioUtilTest, Round) {
30 EXPECT_EQ(0, RoundToInt16(0.f));
31 EXPECT_EQ(0, RoundToInt16(0.4f));
32 EXPECT_EQ(1, RoundToInt16(0.5f));
33 EXPECT_EQ(0, RoundToInt16(-0.4f));
34 EXPECT_EQ(-1, RoundToInt16(-0.5f));
35}
36
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000037TEST(AudioUtilTest, InterleavingStereo) {
38 const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81};
39 const int kSamplesPerChannel = 4;
40 const int kNumChannels = 2;
41 const int kLength = kSamplesPerChannel * kNumChannels;
42 int16_t left[kSamplesPerChannel], right[kSamplesPerChannel];
43 int16_t* deinterleaved[] = {left, right};
44 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved);
45 const int16_t kRefLeft[] = {2, 4, 8, 16};
46 const int16_t kRefRight[] = {3, 9, 27, 81};
47 ExpectArraysEq(left, kRefLeft, kSamplesPerChannel);
48 ExpectArraysEq(right, kRefRight, kSamplesPerChannel);
49
50 int16_t interleaved[kLength];
51 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
52 ExpectArraysEq(interleaved, kInterleaved, kLength);
53}
54
55TEST(AudioUtilTest, InterleavingMonoIsIdentical) {
56 const int16_t kInterleaved[] = {1, 2, 3, 4, 5};
57 const int kSamplesPerChannel = 5;
58 const int kNumChannels = 1;
59 int16_t mono[kSamplesPerChannel];
60 int16_t* deinterleaved[] = {mono};
61 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved);
62 ExpectArraysEq(mono, kInterleaved, kSamplesPerChannel);
63
64 int16_t interleaved[kSamplesPerChannel];
65 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
66 ExpectArraysEq(interleaved, mono, kSamplesPerChannel);
67}
68
69} // namespace webrtc