shithub: openh264

Download patch

ref: d906dda2240b2c4b39687f7474a4d1607319681a
parent: eb9f56584fae81eab9be6ab999040ed5e4a7cfcd
author: Sindre Aamås <saamas@cisco.com>
date: Tue Apr 19 16:50:34 EDT 2016

[UT] Improve GetNonZeroCount tests

Reduce duplication.
Test more combinations.
Always test boundary cases.

--- a/test/encoder/EncUT_EncoderMbAux.cpp
+++ b/test/encoder/EncUT_EncoderMbAux.cpp
@@ -267,26 +267,39 @@
 GENERATE_UT_FOR_COPY (16, 16, WelsCopy16x16NotAligned_sse2);
 GENERATE_UT_FOR_COPY (16, 16, WelsCopy16x16_sse2);
 #endif
-TEST (EncodeMbAuxTest, WelsGetNoneZeroCount_c) {
+
+namespace {
+
+void TestGetNoneZeroCount (PGetNoneZeroCountFunc func) {
   ENFORCE_STACK_ALIGN_1D (int16_t, pLevel, 16, 16);
-  int32_t result = 0;
-  for (int i = 0; i < 16; i++) {
-    pLevel[i] = (rand() & 0x07) - 4;
-    if (pLevel[i]) result ++;
+  const int num_test_runs = 1000;
+  for (int run = 0; run < num_test_runs; run++) {
+    const bool all_zero = run == 0;
+    const bool all_nonzero = run == 1;
+    int result = 0;
+    for (int i = 0; i < 16; i++) {
+      const int r = rand();
+      if (all_zero)
+        pLevel[i] = 0;
+      else if (all_nonzero)
+        pLevel[i] = r % 0xFFFF - 0x8000 ? r % 0xFFFF - 0x8000 : 0x7FFF;
+      else
+        pLevel[i] = (r >> 16 & 1) * ((r & 0xFFFF) - 0x8000);
+      result += pLevel[i] != 0;
+    }
+    const int32_t nnz = func (pLevel);
+    EXPECT_EQ (nnz, result);
   }
-  int32_t nnz = WelsGetNoneZeroCount_c (pLevel);
-  EXPECT_EQ (nnz, result);
 }
+
+} // anon ns.
+
+TEST (EncodeMbAuxTest, WelsGetNoneZeroCount_c) {
+  TestGetNoneZeroCount (WelsGetNoneZeroCount_c);
+}
 #ifdef X86_ASM
 TEST (EncodeMbAuxTest, WelsGetNoneZeroCount_sse2) {
-  ENFORCE_STACK_ALIGN_1D (int16_t, pLevel, 16, 16);
-  int32_t result = 0;
-  for (int i = 0; i < 16; i++) {
-    pLevel[i] = (rand() & 0x07) - 4;
-    if (pLevel[i]) result ++;
-  }
-  int32_t nnz = WelsGetNoneZeroCount_sse2 (pLevel);
-  EXPECT_EQ (nnz, result);
+  TestGetNoneZeroCount (WelsGetNoneZeroCount_sse2);
 }
 #endif
 #define WELS_ABS_LC(a) ((sign ^ (int32_t)(a)) - sign)