Bug 1315561 - gtestify dom/media/compiledtest/*.cpp. r=padenot.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 09 Nov 2016 08:47:23 +1100
changeset 351837 74a75900ef5e03e86ccd5661002f4bc4392116cf
parent 351836 ef2f15749bb95bcca8eb4f1cdb9aa9cdfe8ac378
child 351838 7245abb243756fdc0b13a09417db08376904620e
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1315561
milestone52.0a1
Bug 1315561 - gtestify dom/media/compiledtest/*.cpp. r=padenot. The only complication was that I had to add namespaces to TestAudioMixer.cpp and TestAudioSegment.cpp to avoid multiple definitions of a few small helper functions like GetLowValue().
dom/media/compiledtest/TestAudioBuffers.cpp
dom/media/compiledtest/TestAudioMixer.cpp
dom/media/compiledtest/TestAudioPacketizer.cpp
dom/media/compiledtest/TestAudioSegment.cpp
dom/media/compiledtest/moz.build
dom/media/gtest/TestAudioBuffers.cpp
dom/media/gtest/TestAudioMixer.cpp
dom/media/gtest/TestAudioPacketizer.cpp
dom/media/gtest/TestAudioSegment.cpp
dom/media/gtest/TestRust.cpp
dom/media/gtest/moz.build
dom/media/moz.build
testing/cppunittest.ini
deleted file mode 100644
--- a/dom/media/compiledtest/moz.build
+++ /dev/null
@@ -1,20 +0,0 @@
-# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
-# vim: set filetype=python:
-# This Source Code Form is subject to the terms of the Mozilla Public
-# License, v. 2.0. If a copy of the MPL was not distributed with this
-# file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
-GeckoCppUnitTests([
-    'TestAudioBuffers',
-    'TestAudioMixer',
-    'TestAudioPacketizer',
-    'TestAudioSegment'
-])
-
-LOCAL_INCLUDES += [
-    '..',
-]
-
-USE_LIBS += [
-    'lgpllibs',
-]
rename from dom/media/compiledtest/TestAudioBuffers.cpp
rename to dom/media/gtest/TestAudioBuffers.cpp
--- a/dom/media/compiledtest/TestAudioBuffers.cpp
+++ b/dom/media/gtest/TestAudioBuffers.cpp
@@ -1,59 +1,57 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <stdint.h>
 #include "AudioBufferUtils.h"
-#include <mozilla/Assertions.h>
+#include "gtest/gtest.h"
 
 const uint32_t FRAMES = 256;
 const uint32_t CHANNELS = 2;
 const uint32_t SAMPLES = CHANNELS * FRAMES;
 
-int main() {
+TEST(AudioBuffers, Test)
+{
   mozilla::AudioCallbackBufferWrapper<float, CHANNELS> mBuffer;
   mozilla::SpillBuffer<float, 128, CHANNELS> b;
   float fromCallback[SAMPLES];
   float other[SAMPLES];
 
   for (uint32_t i = 0; i < SAMPLES; i++) {
     other[i] = 1.0;
     fromCallback[i] = 0.0;
   }
 
   // Set the buffer in the wrapper from the callback
   mBuffer.SetBuffer(fromCallback, FRAMES);
 
   // Fill the SpillBuffer with data.
-  MOZ_RELEASE_ASSERT(b.Fill(other, 15) == 15);
-  MOZ_RELEASE_ASSERT(b.Fill(other, 17) == 17);
+  ASSERT_TRUE(b.Fill(other, 15) == 15);
+  ASSERT_TRUE(b.Fill(other, 17) == 17);
   for (uint32_t i = 0; i < 32 * CHANNELS; i++) {
     other[i] = 0.0;
   }
 
   // Empty it in the AudioCallbackBufferWrapper
-  MOZ_RELEASE_ASSERT(b.Empty(mBuffer) == 32);
+  ASSERT_TRUE(b.Empty(mBuffer) == 32);
 
   // Check available return something reasonnable
-  MOZ_RELEASE_ASSERT(mBuffer.Available() == FRAMES - 32);
+  ASSERT_TRUE(mBuffer.Available() == FRAMES - 32);
 
   // Fill the buffer with the rest of the data
   mBuffer.WriteFrames(other + 32 * CHANNELS, FRAMES - 32);
 
   // Check the buffer is now full
-  MOZ_RELEASE_ASSERT(mBuffer.Available() == 0);
+  ASSERT_TRUE(mBuffer.Available() == 0);
 
   for (uint32_t i = 0 ; i < SAMPLES; i++) {
-    if (fromCallback[i] != 1.0) {
-      fprintf(stderr, "Difference at %d (%f != %f)\n", i, fromCallback[i], 1.0);
-      MOZ_CRASH("Samples differ");
-    }
+    ASSERT_TRUE(fromCallback[i] == 1.0) <<
+      "Difference at " << i << " (" << fromCallback[i] << " != " << 1.0 <<
+      ")\n";
   }
 
-  MOZ_RELEASE_ASSERT(b.Fill(other, FRAMES) == 128);
-  MOZ_RELEASE_ASSERT(b.Fill(other, FRAMES) == 0);
-  MOZ_RELEASE_ASSERT(b.Empty(mBuffer) == 0);
-
-  return 0;
+  ASSERT_TRUE(b.Fill(other, FRAMES) == 128);
+  ASSERT_TRUE(b.Fill(other, FRAMES) == 0);
+  ASSERT_TRUE(b.Empty(mBuffer) == 0);
 }
rename from dom/media/compiledtest/TestAudioMixer.cpp
rename to dom/media/gtest/TestAudioMixer.cpp
--- a/dom/media/compiledtest/TestAudioMixer.cpp
+++ b/dom/media/gtest/TestAudioMixer.cpp
@@ -1,18 +1,21 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AudioMixer.h"
+#include "gtest/gtest.h"
 
 using mozilla::AudioDataValue;
 using mozilla::AudioSampleFormat;
 
+namespace audio_mixer {
+
 struct MixerConsumer : public mozilla::MixerCallbackReceiver
 {
 /* In this test, the different audio stream and channels are always created to
  * cancel each other. */
   void MixerCallback(AudioDataValue* aData, AudioSampleFormat aFormat, uint32_t aChannels, uint32_t aFrames, uint32_t aSampleRate)
   {
     bool silent = true;
     for (uint32_t i = 0; i < aChannels * aFrames; i++) {
@@ -20,19 +23,17 @@ struct MixerConsumer : public mozilla::M
         if (aFormat == mozilla::AUDIO_FORMAT_S16) {
           fprintf(stderr, "Sample at %d is not silent: %d\n", i, (short)aData[i]);
         } else {
           fprintf(stderr, "Sample at %d is not silent: %f\n", i, (float)aData[i]);
         }
         silent = false;
       }
     }
-    if (!silent) {
-      MOZ_CRASH();
-    }
+    ASSERT_TRUE(silent);
   }
 };
 
 /* Helper function to give us the maximum and minimum value that don't clip,
  * for a given sample format (integer or floating-point). */
 template<typename T>
 T GetLowValue();
 
@@ -62,17 +63,18 @@ short GetHighValue<short>() {
 void FillBuffer(AudioDataValue* aBuffer, uint32_t aLength, AudioDataValue aValue)
 {
   AudioDataValue* end = aBuffer + aLength;
   while (aBuffer != end) {
     *aBuffer++ = aValue;
   }
 }
 
-int main(int argc, char* argv[]) {
+TEST(AudioMixer, Test)
+{
   const uint32_t CHANNEL_LENGTH = 256;
   const uint32_t AUDIO_RATE = 44100;
   MixerConsumer consumer;
   AudioDataValue a[CHANNEL_LENGTH * 2];
   AudioDataValue b[CHANNEL_LENGTH * 2];
   FillBuffer(a, CHANNEL_LENGTH, GetLowValue<AudioDataValue>());
   FillBuffer(a + CHANNEL_LENGTH, CHANNEL_LENGTH, GetHighValue<AudioDataValue>());
   FillBuffer(b, CHANNEL_LENGTH, GetHighValue<AudioDataValue>());
@@ -153,11 +155,11 @@ int main(int argc, char* argv[]) {
     mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE);
     mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE);
     mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE);
     mixer.FinishMixing();
     mixer.Mix(a, 2, CHANNEL_LENGTH, AUDIO_RATE);
     mixer.Mix(b, 2, CHANNEL_LENGTH, AUDIO_RATE);
     mixer.FinishMixing();
   }
+}
 
-  return 0;
-}
+} // namespace audio_mixer
rename from dom/media/compiledtest/TestAudioPacketizer.cpp
rename to dom/media/gtest/TestAudioPacketizer.cpp
--- a/dom/media/compiledtest/TestAudioPacketizer.cpp
+++ b/dom/media/gtest/TestAudioPacketizer.cpp
@@ -1,17 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <stdint.h>
 #include <math.h>
 #include "../AudioPacketizer.h"
-#include <mozilla/Assertions.h>
+#include "gtest/gtest.h"
 
 using namespace mozilla;
 
 template<typename T>
 class AutoBuffer
 {
 public:
   explicit AutoBuffer(size_t aLength)
@@ -35,39 +35,36 @@ int16_t Sequence(int16_t* aBuffer, uint3
     aBuffer[i] = aStart + i;
   }
   return aStart + i;
 }
 
 void IsSequence(int16_t* aBuffer, uint32_t aSize, uint32_t aStart = 0)
 {
   for (uint32_t i = 0; i < aSize; i++) {
-    if (aBuffer[i] != static_cast<int64_t>(aStart + i)) {
-      fprintf(stderr, "Buffer is not a sequence at offset %u\n", i);
-      MOZ_CRASH("Buffer is not a sequence");
-    }
+    ASSERT_TRUE(aBuffer[i] == static_cast<int64_t>(aStart + i)) <<
+      "Buffer is not a sequence at offset " << i << std::endl;
   }
   // Buffer is a sequence.
 }
 
 void Zero(int16_t* aBuffer, uint32_t aSize)
 {
   for (uint32_t i = 0; i < aSize; i++) {
-    if (aBuffer[i] != 0) {
-      fprintf(stderr, "Buffer is not null at offset %u\n", i);
-      MOZ_CRASH("Buffer is not null");
-    }
+    ASSERT_TRUE(aBuffer[i] == 0) <<
+      "Buffer is not null at offset " << i << std::endl;
   }
 }
 
 double sine(uint32_t aPhase) {
- return sin(aPhase * 2 * M_PI * 440 / 44100);
+  return sin(aPhase * 2 * M_PI * 440 / 44100);
 }
 
-int main() {
+TEST(AudioPacketizer, Test)
+{
   for (int16_t channels = 1; channels < 2; channels++) {
     // Test that the packetizer returns zero on underrun
     {
       AudioPacketizer<int16_t, int16_t> ap(441, channels);
       for (int16_t i = 0; i < 10; i++) {
         int16_t* out = ap.Output();
         Zero(out, 441);
         delete[] out;
@@ -152,22 +149,19 @@ int main() {
           }
           phase++;
         }
         ap.Input(b.Get(), 128);
         while (ap.PacketsAvailable()) {
           int16_t* packet = ap.Output();
           for (uint32_t k = 0; k < ap.PacketSize(); k++) {
             for (int32_t c = 0; c < channels; c++) {
-              MOZ_RELEASE_ASSERT(packet[k * channels + c] ==
-                                 static_cast<int16_t>(((2 << 14) * sine(outPhase))));
+              ASSERT_TRUE(packet[k * channels + c] ==
+                          static_cast<int16_t>(((2 << 14) * sine(outPhase))));
             }
             outPhase++;
           }
           delete [] packet;
         }
       }
     }
   }
-
-  printf("OK\n");
-  return 0;
 }
rename from dom/media/compiledtest/TestAudioSegment.cpp
rename to dom/media/gtest/TestAudioSegment.cpp
--- a/dom/media/compiledtest/TestAudioSegment.cpp
+++ b/dom/media/gtest/TestAudioSegment.cpp
@@ -1,26 +1,20 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AudioSegment.h"
 #include <iostream>
-#include <mozilla/Assertions.h>
+#include "gtest/gtest.h"
 
 using namespace mozilla;
 
-namespace mozilla {
-uint32_t
-GetAudioChannelsSuperset(uint32_t aChannels1, uint32_t aChannels2)
-{
-  return std::max(aChannels1, aChannels2);
-}
-}
+namespace audio_segment {
 
 /* Helper function to give us the maximum and minimum value that don't clip,
  * for a given sample format (integer or floating-point). */
 template<typename T>
 T GetLowValue();
 
 template<typename T>
 T GetHighValue();
@@ -137,18 +131,18 @@ void TestInterleaveAndConvert()
   for (uint32_t channels = 1; channels < maxChannels; channels++) {
     const SrcT* const* src = GetPlanarChannelArray<SrcT>(channels, arraySize);
     DstT* dst = new DstT[channels * arraySize];
 
     InterleaveAndConvertBuffer(src, arraySize, 1.0, channels, dst);
 
     uint32_t channelIndex = 0;
     for (size_t i = 0; i < arraySize * channels; i++) {
-      MOZ_RELEASE_ASSERT(FuzzyEqual(dst[i],
-                         FloatToAudioSample<DstT>(1. / (channelIndex + 1))));
+      ASSERT_TRUE(FuzzyEqual(dst[i],
+                  FloatToAudioSample<DstT>(1. / (channelIndex + 1))));
       channelIndex++;
       channelIndex %= channels;
     }
 
     DeletePlanarChannelsArray(src, channels);
     delete [] dst;
   }
 }
@@ -161,18 +155,18 @@ void TestDeinterleaveAndConvert()
   for (uint32_t channels = 1; channels < maxChannels; channels++) {
     const SrcT* src = GetInterleavedChannelArray<SrcT>(channels, arraySize);
     DstT** dst = GetPlanarArray<DstT>(channels, arraySize);
 
     DeinterleaveAndConvertBuffer(src, arraySize, channels, dst);
 
     for (size_t channel = 0; channel < channels; channel++) {
       for (size_t i = 0; i < arraySize; i++) {
-        MOZ_RELEASE_ASSERT(FuzzyEqual(dst[channel][i],
-                           FloatToAudioSample<DstT>(1. / (channel + 1))));
+        ASSERT_TRUE(FuzzyEqual(dst[channel][i],
+                    FloatToAudioSample<DstT>(1. / (channel + 1))));
       }
     }
 
     DeleteInterleavedChannelArray(src);
     DeletePlanarArray(dst, channels);
   }
 }
 
@@ -196,21 +190,21 @@ void TestUpmixStereo()
 
   channels[0] = new T[arraySize];
 
   for (size_t i = 0; i < arraySize; i++) {
     channels[0][i] = GetHighValue<T>();
   }
   channelsptr[0] = channels[0];
 
-  AudioChannelsUpMix(&channelsptr, 2, ::SilentChannel<T>());
+  AudioChannelsUpMix(&channelsptr, 2, SilentChannel<T>());
 
   for (size_t channel = 0; channel < 2; channel++) {
     for (size_t i = 0; i < arraySize; i++) {
-      MOZ_RELEASE_ASSERT(channelsptr[channel][i] == GetHighValue<T>());
+      ASSERT_TRUE(channelsptr[channel][i] == GetHighValue<T>());
     }
   }
   delete channels[0];
 }
 
 template<typename T>
 void TestDownmixStereo()
 {
@@ -231,32 +225,33 @@ void TestDownmixStereo()
       input[channel][i] = channel == 0 ? GetLowValue<T>() : GetHighValue<T>();
     }
     inputptr[channel] = input[channel];
   }
 
   AudioChannelsDownMix(inputptr, output, 1, arraySize);
 
   for (size_t i = 0; i < arraySize; i++) {
-    MOZ_RELEASE_ASSERT(output[0][i] == GetSilentValue<T>());
-    MOZ_RELEASE_ASSERT(output[0][i] == GetSilentValue<T>());
+    ASSERT_TRUE(output[0][i] == GetSilentValue<T>());
+    ASSERT_TRUE(output[0][i] == GetSilentValue<T>());
   }
 
   delete output[0];
   delete output;
 }
 
-int main(int argc, char* argv[]) {
+TEST(AudioSegment, Test)
+{
   TestInterleaveAndConvert<float, float>();
   TestInterleaveAndConvert<float, int16_t>();
   TestInterleaveAndConvert<int16_t, float>();
   TestInterleaveAndConvert<int16_t, int16_t>();
   TestDeinterleaveAndConvert<float, float>();
   TestDeinterleaveAndConvert<float, int16_t>();
   TestDeinterleaveAndConvert<int16_t, float>();
   TestDeinterleaveAndConvert<int16_t, int16_t>();
   TestUpmixStereo<float>();
   TestUpmixStereo<int16_t>();
   TestDownmixStereo<float>();
   TestDownmixStereo<int16_t>();
+}
 
-  return 0;
-}
+} // namespace audio_segment
--- a/dom/media/gtest/TestRust.cpp
+++ b/dom/media/gtest/TestRust.cpp
@@ -1,8 +1,9 @@
 #include <stdint.h>
+#include "gtest/gtest.h"
 
 extern "C" uint8_t* test_rust();
 
 TEST(rust, CallFromCpp) {
   auto greeting = test_rust();
   EXPECT_STREQ(reinterpret_cast<char*>(greeting), "hello from rust.");
 }
--- a/dom/media/gtest/moz.build
+++ b/dom/media/gtest/moz.build
@@ -1,17 +1,21 @@
 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 UNIFIED_SOURCES += [
     'MockMediaResource.cpp',
+    'TestAudioBuffers.cpp',
     'TestAudioCompactor.cpp',
+    'TestAudioMixer.cpp',
+    'TestAudioPacketizer.cpp',
+    'TestAudioSegment.cpp',
     'TestGMPCrossOrigin.cpp',
     'TestGMPRemoveAndDelete.cpp',
     'TestGMPUtils.cpp',
     'TestIntervalSet.cpp',
     'TestMediaDataDecoder.cpp',
     'TestMediaEventSource.cpp',
     'TestMediaFormatReader.cpp',
     'TestMozPromise.cpp',
--- a/dom/media/moz.build
+++ b/dom/media/moz.build
@@ -50,17 +50,16 @@ if CONFIG['MOZ_ANDROID_OMX']:
 
 if CONFIG['MOZ_FMP4']:
     DIRS += ['fmp4']
 
 if CONFIG['MOZ_WEBRTC']:
     DIRS += ['bridge']
 
 TEST_DIRS += [
-    'compiledtest',
     'gtest',
 ]
 
 MOCHITEST_MANIFESTS += [
     'test/mochitest.ini',
     'tests/mochitest/identity/mochitest.ini',
 ]
 
--- a/testing/cppunittest.ini
+++ b/testing/cppunittest.ini
@@ -1,16 +1,13 @@
 [ShowSSEConfig]
 [TestAppShellSteadyState]
 [TestArray]
 [TestArrayUtils]
 [TestAtomics]
-[TestAudioBuffers]
-skip-if = os == 'b2g'  # Bug 1062937
-[TestAudioMixer]
 [TestBinarySearch]
 [TestBind]
 [TestBloomFilter]
 [TestCOM]
 skip-if = os != 'win'
 [TestCasting]
 [TestCeilingFloor]
 [TestCertDB]