From 69d41f144ad82d652f148c303a168cd07d222d41 Mon Sep 17 00:00:00 2001 From: dvdli Date: Wed, 2 Dec 2020 15:20:20 +0800 Subject: fix typo, add comments to describe the tests and fix the loopback test --- tests/include/pcm_test_device.h | 2 +- tests/src/mixer_test.cc | 21 ++++++++++++++------- tests/src/pcm_in_test.cc | 4 ++-- tests/src/pcm_loopback_test.cc | 23 +++++++++++++++++------ tests/src/pcm_out_test.cc | 2 +- tests/src/pcm_params_test.cc | 5 +++-- tests/src/pcm_test.cc | 2 +- 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/tests/include/pcm_test_device.h b/tests/include/pcm_test_device.h index 2df7c93..7ced192 100644 --- a/tests/include/pcm_test_device.h +++ b/tests/include/pcm_test_device.h @@ -49,6 +49,6 @@ constexpr unsigned int kLoopbackPlaybackDevice = TEST_LOOPBACK_PLAYBACK_DEVICE; constexpr unsigned int kLoopbackCaptureDevice = TEST_LOOPBACK_CAPTURE_DEVICE; } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa #endif diff --git a/tests/src/mixer_test.cc b/tests/src/mixer_test.cc index d4166ab..717269c 100644 --- a/tests/src/mixer_test.cc +++ b/tests/src/mixer_test.cc @@ -47,6 +47,9 @@ namespace testing { static constexpr unsigned int kMaxCardIndex = MAX_CARD_INDEX; +static constexpr int k100Percent = 100; +static constexpr int k0Percent = 0; + TEST(MixerTest, OpenAndClose) { ASSERT_EQ(mixer_open(1000), nullptr); mixer_close(nullptr); @@ -214,10 +217,10 @@ TEST_P(MixerControlsTest, GetPercent) { int max = mixer_ctl_get_range_max(control); int min = mixer_ctl_get_range_min(control); int percent = mixer_ctl_get_percent(control, value_id); - ASSERT_GE(percent, 0); - ASSERT_LE(percent, 100); + ASSERT_GE(percent, k0Percent); + ASSERT_LE(percent, k100Percent); int range = max - min; - ASSERT_EQ(percent, (values[value_id] - min) * 100 / range); + ASSERT_EQ(percent, (values[value_id] - min) * k100Percent / range); } } else { ASSERT_EQ(mixer_ctl_get_percent(control, 0), -EINVAL); @@ -237,14 +240,16 @@ TEST_P(MixerControlsTest, SetPercent) { int min = mixer_ctl_get_range_min(control); int value = values[value_id]; int percent = mixer_ctl_get_percent(control, value_id); - if (mixer_ctl_set_percent(const_cast(control), value_id, 100) == 0) { + if (mixer_ctl_set_percent( + const_cast(control), value_id, k100Percent) == 0) { // note: some controls are able to be written, but their values might not be // changed. mixer_ctl_get_array(control, values.get(), number_of_values); int new_value = values[value_id]; ASSERT_TRUE(new_value == value || new_value == max); } - if (mixer_ctl_set_percent(const_cast(control), value_id, 0) == 0) { + if (mixer_ctl_set_percent( + const_cast(control), value_id, k0Percent) == 0) { mixer_ctl_get_array(control, values.get(), number_of_values); int new_value = values[value_id]; ASSERT_TRUE(new_value == value || new_value == min); @@ -276,7 +281,9 @@ TEST_P(MixerControlsTest, Event) { int percent = mixer_ctl_get_percent(control, 0); std::thread thread([local_mixer_object, control, percent] () { std::this_thread::sleep_for(std::chrono::milliseconds(50)); - mixer_ctl_set_percent(const_cast(control), 0, percent == 100 ? 0 : 100); + mixer_ctl_set_percent( + const_cast(control), 0, + percent == k100Percent ? k0Percent : k100Percent); }); EXPECT_EQ(mixer_wait_event(mixer_object, 1000), 1); @@ -306,4 +313,4 @@ INSTANTIATE_TEST_SUITE_P( )); } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa diff --git a/tests/src/pcm_in_test.cc b/tests/src/pcm_in_test.cc index 719e4ca..e912abb 100644 --- a/tests/src/pcm_in_test.cc +++ b/tests/src/pcm_in_test.cc @@ -72,7 +72,7 @@ class PcmInTest : public ::testing::Test { pcm* pcm_object; }; -TEST_F(PcmInTest, GetDealy) { +TEST_F(PcmInTest, GetDelay) { long delay = pcm_get_delay(pcm_object); std::cout << delay << std::endl; ASSERT_GE(delay, 0); @@ -111,4 +111,4 @@ TEST_F(PcmInTest, Writei) { } } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa diff --git a/tests/src/pcm_loopback_test.cc b/tests/src/pcm_loopback_test.cc index 2571c58..6a3ffb8 100644 --- a/tests/src/pcm_loopback_test.cc +++ b/tests/src/pcm_loopback_test.cc @@ -144,6 +144,9 @@ TEST(PcmLoopbackTest, LoopbackS16le) { static constexpr unsigned int kDefaultSamplingRate = 48000; static constexpr unsigned int kDefaultPeriodSize = 1024; static constexpr unsigned int kDefaultPeriodCount = 3; + static constexpr unsigned int kDefaultPeriodTimeInMs = + kDefaultPeriodSize * 1000 / kDefaultSamplingRate; + static constexpr pcm_config kInConfig = { .channels = kDefaultChannels, .rate = kDefaultSamplingRate, @@ -183,12 +186,20 @@ TEST(PcmLoopbackTest, LoopbackS16le) { auto buffer = std::make_unique(buffer_size); int32_t counter = 0; while (!stopping) { - EXPECT_EQ(pcm_readi(pcm_in, buffer.get(), frames), frames); - counter++; + int res = pcm_readi(pcm_in, buffer.get(), frames); + if (res == -1) { + std::cout << pcm_get_error(pcm_in) << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(kDefaultPeriodTimeInMs)); + continue; + } + EXPECT_EQ(pcm_readi(pcm_in, buffer.get(), frames), frames) << counter; + // Test the energy of the buffer after the sine tone samples fill in the buffer. + // Therefore, check the buffer 5 times later. if (counter >= 5) { double e = Energy(buffer.get(), frames * kInConfig.channels); - EXPECT_GT(e, 0.0); + EXPECT_GT(e, 0.0) << counter; } + counter++; } }); @@ -196,11 +207,11 @@ TEST(PcmLoopbackTest, LoopbackS16le) { SineToneGenerator<2, 48000, 1000, 0, PCM_FORMAT_S16_LE> generator; size_t buffer_size = pcm_frames_to_bytes(pcm_out, kDefaultPeriodSize); unsigned int frames = pcm_bytes_to_frames(pcm_out, buffer_size); - int32_t counter = 0; auto buffer = std::make_unique(buffer_size); + int32_t counter = 0; while (!stopping) { generator.Read(buffer.get(), buffer_size); - EXPECT_EQ(pcm_writei(pcm_out, buffer.get(), frames), frames); + EXPECT_EQ(pcm_writei(pcm_out, buffer.get(), frames), frames) << counter; counter++; } }); @@ -216,4 +227,4 @@ TEST(PcmLoopbackTest, LoopbackS16le) { } } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa diff --git a/tests/src/pcm_out_test.cc b/tests/src/pcm_out_test.cc index 5939565..cbc6983 100644 --- a/tests/src/pcm_out_test.cc +++ b/tests/src/pcm_out_test.cc @@ -213,4 +213,4 @@ TEST_F(PcmOutMmapTest, Write) { } } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa diff --git a/tests/src/pcm_params_test.cc b/tests/src/pcm_params_test.cc index 18c3c2c..c8151e1 100644 --- a/tests/src/pcm_params_test.cc +++ b/tests/src/pcm_params_test.cc @@ -40,8 +40,9 @@ namespace tinyalsa { namespace testing { static inline unsigned int OrAllBits(const pcm_mask *mask) { + static constexpr size_t kTotalMaskBytes = 32; unsigned int res = 0; - for (uint32_t i = 0; i < 32 / sizeof(unsigned int); ++i) { + for (uint32_t i = 0; i < kTotalMaskBytes / sizeof(pcm_mask::bits[0]); ++i) { res |= mask->bits[i]; } return res; @@ -218,4 +219,4 @@ TEST(PcmParamsTest, GetPlaybackDeviceParams) { } } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa diff --git a/tests/src/pcm_test.cc b/tests/src/pcm_test.cc index cf88fb7..2668350 100644 --- a/tests/src/pcm_test.cc +++ b/tests/src/pcm_test.cc @@ -100,4 +100,4 @@ TEST(PcmTest, OpenAndCloseOutPcm) { } } // namespace testing -} // namespace tinyalse +} // namespace tinyalsa -- cgit v1.2.3