1 /* 2 * Copyright (c) 2011 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 12 /* 13 * This file includes the implementation of the VAD unit tests. 14 */ 15 16 #include <cstring> 17 #include "unit_test.h" 18 #include "webrtc_vad.h" 19 20 21 class VadEnvironment : public ::testing::Environment { 22 public: 23 virtual void SetUp() { 24 } 25 26 virtual void TearDown() { 27 } 28 }; 29 30 VadTest::VadTest() 31 { 32 } 33 34 void VadTest::SetUp() { 35 } 36 37 void VadTest::TearDown() { 38 } 39 40 TEST_F(VadTest, ApiTest) { 41 VadInst *vad_inst; 42 int i, j, k; 43 short zeros[960]; 44 short speech[960]; 45 char version[32]; 46 47 // Valid test cases 48 int fs[3] = {8000, 16000, 32000}; 49 int nMode[4] = {0, 1, 2, 3}; 50 int framelen[3][3] = {{80, 160, 240}, 51 {160, 320, 480}, {320, 640, 960}} ; 52 int vad_counter = 0; 53 54 memset(zeros, 0, sizeof(short) * 960); 55 memset(speech, 1, sizeof(short) * 960); 56 speech[13] = 1374; 57 speech[73] = -3747; 58 59 60 61 // WebRtcVad_get_version() 62 WebRtcVad_get_version(version); 63 //printf("API Test for %s\n", version); 64 65 // Null instance tests 66 EXPECT_EQ(-1, WebRtcVad_Create(NULL)); 67 EXPECT_EQ(-1, WebRtcVad_Init(NULL)); 68 EXPECT_EQ(-1, WebRtcVad_Assign(NULL, NULL)); 69 EXPECT_EQ(-1, WebRtcVad_Free(NULL)); 70 EXPECT_EQ(-1, WebRtcVad_set_mode(NULL, nMode[0])); 71 EXPECT_EQ(-1, WebRtcVad_Process(NULL, fs[0], speech, framelen[0][0])); 72 73 74 EXPECT_EQ(WebRtcVad_Create(&vad_inst), 0); 75 76 // Not initialized tests 77 EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, fs[0], speech, framelen[0][0])); 78 EXPECT_EQ(-1, WebRtcVad_set_mode(vad_inst, nMode[0])); 79 80 // WebRtcVad_Init() tests 81 EXPECT_EQ(WebRtcVad_Init(vad_inst), 0); 82 83 // WebRtcVad_set_mode() tests 84 EXPECT_EQ(-1, WebRtcVad_set_mode(vad_inst, -1)); 85 EXPECT_EQ(-1, WebRtcVad_set_mode(vad_inst, 4)); 86 87 for (i = 0; i < sizeof(nMode)/sizeof(nMode[0]); i++) { 88 EXPECT_EQ(WebRtcVad_set_mode(vad_inst, nMode[i]), 0); 89 } 90 91 // WebRtcVad_Process() tests 92 EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, fs[0], NULL, framelen[0][0])); 93 EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, 12000, speech, framelen[0][0])); 94 EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, fs[0], speech, framelen[1][1])); 95 EXPECT_EQ(WebRtcVad_Process(vad_inst, fs[0], zeros, framelen[0][0]), 0); 96 for (i = 0; i < sizeof(fs)/sizeof(fs[0]); i++) { 97 for (j = 0; j < sizeof(framelen[0])/sizeof(framelen[0][0]); j++) { 98 for (k = 0; k < sizeof(nMode)/sizeof(nMode[0]); k++) { 99 EXPECT_EQ(WebRtcVad_set_mode(vad_inst, nMode[k]), 0); 100 // printf("%d\n", WebRtcVad_Process(vad_inst, fs[i], speech, framelen[i][j])); 101 if (vad_counter < 9) 102 { 103 EXPECT_EQ(WebRtcVad_Process(vad_inst, fs[i], speech, framelen[i][j]), 1); 104 } else 105 { 106 EXPECT_EQ(WebRtcVad_Process(vad_inst, fs[i], speech, framelen[i][j]), 0); 107 } 108 vad_counter++; 109 } 110 } 111 } 112 113 EXPECT_EQ(0, WebRtcVad_Free(vad_inst)); 114 115 } 116 117 int main(int argc, char** argv) { 118 ::testing::InitGoogleTest(&argc, argv); 119 VadEnvironment* env = new VadEnvironment; 120 ::testing::AddGlobalTestEnvironment(env); 121 122 return RUN_ALL_TESTS(); 123 } 124