Bug 1573184 - Add gtests to BenchmarkStorage classes. r=jya

Differential Revision: https://phabricator.services.mozilla.com/D41580

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alex Chronopoulos 2019-08-13 05:14:22 +00:00
Родитель adea85c408
Коммит 590d647d81
4 изменённых файлов: 105 добавлений и 3 удалений

Просмотреть файл

@ -0,0 +1,92 @@
/* -*- 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 "mozilla/BenchmarkStorageParent.h"
#include "gmock/gmock.h"
#include "gtest/gtest-printers.h"
#include "gtest/gtest.h"
using ::testing::Return;
using namespace mozilla;
TEST(BenchmarkStorage, MovingAverage)
{
int32_t av = 0;
int32_t win = 0;
int32_t val = 100;
BenchmarkStorageParent::MovingAverage(av, win, val);
EXPECT_EQ(av, val) << "1st average";
EXPECT_EQ(win, 1) << "1st window";
av = 50;
win = 1;
val = 100;
BenchmarkStorageParent::MovingAverage(av, win, val);
EXPECT_EQ(av, 75) << "2nd average";
EXPECT_EQ(win, 2) << "2nd window";
av = 100;
win = 9;
val = 90;
BenchmarkStorageParent::MovingAverage(av, win, val);
EXPECT_EQ(av, 99) << "9th average";
EXPECT_EQ(win, 10) << "9th window";
av = 90;
win = 19;
val = 90;
BenchmarkStorageParent::MovingAverage(av, win, val);
EXPECT_EQ(av, 90) << "19th average";
EXPECT_EQ(win, 20) << "19th window";
av = 90;
win = 20;
val = 100;
BenchmarkStorageParent::MovingAverage(av, win, val);
EXPECT_EQ(av, 91) << "20th average";
EXPECT_EQ(win, 20) << "20th window";
}
TEST(BenchmarkStorage, ParseStoredValue)
{
int32_t win = 0;
int32_t score = BenchmarkStorageParent::ParseStoredValue(1100, win);
EXPECT_EQ(win, 1) << "Window";
EXPECT_EQ(score, 100) << "Score/Percentage";
win = 0;
score = BenchmarkStorageParent::ParseStoredValue(10099, win);
EXPECT_EQ(win, 10) << "Window";
EXPECT_EQ(score, 99) << "Score/Percentage";
win = 0;
score = BenchmarkStorageParent::ParseStoredValue(15038, win);
EXPECT_EQ(win, 15) << "Window";
EXPECT_EQ(score, 38) << "Score/Percentage";
win = 0;
score = BenchmarkStorageParent::ParseStoredValue(20099, win);
EXPECT_EQ(win, 20) << "Window";
EXPECT_EQ(score, 99) << "Score/Percentage";
}
TEST(BenchmarkStorage, PrepareStoredValue)
{
int32_t stored_value = BenchmarkStorageParent::PrepareStoredValue(80, 1);
EXPECT_EQ(stored_value, 1080) << "Window";
stored_value = BenchmarkStorageParent::PrepareStoredValue(100, 6);
EXPECT_EQ(stored_value, 6100) << "Window";
stored_value = BenchmarkStorageParent::PrepareStoredValue(1, 10);
EXPECT_EQ(stored_value, 10001) << "Window";
stored_value = BenchmarkStorageParent::PrepareStoredValue(88, 13);
EXPECT_EQ(stored_value, 13088) << "Window";
stored_value = BenchmarkStorageParent::PrepareStoredValue(100, 20);
EXPECT_EQ(stored_value, 20100) << "Window";
}

Просмотреть файл

@ -23,6 +23,7 @@ UNIFIED_SOURCES += [
'TestAudioPacketizer.cpp',
'TestAudioSegment.cpp',
'TestAudioTrackEncoder.cpp',
'TestBenchmarkStorage.cpp',
'TestBitWriter.cpp',
'TestBlankVideoDataCreator.cpp',
'TestBufferReader.cpp',

Просмотреть файл

@ -15,7 +15,8 @@ const int32_t AVG_WINDOW = 20;
* aWindow and the already existing average aAverage. When the method returns
* aAverage will contain the new average and aWindow will contain the new
* window.*/
void MovingAverage(int32_t& aAverage, int32_t& aWindow, const int32_t aValue) {
void BenchmarkStorageParent::MovingAverage(int32_t& aAverage, int32_t& aWindow,
const int32_t aValue) {
if (aWindow < AVG_WINDOW) {
aAverage = (aAverage * aWindow + aValue) / (aWindow + 1);
aWindow++;
@ -39,7 +40,8 @@ void MovingAverage(int32_t& aAverage, int32_t& aWindow, const int32_t aValue) {
* (19) and the average score (98). The aValue will
* be parsed, the aWindow will contain the window (of the moving average) and
* the return value will contain the average itself. */
int32_t ParseStoredValue(int32_t aValue, int32_t& aWindow) {
int32_t BenchmarkStorageParent::ParseStoredValue(int32_t aValue,
int32_t& aWindow) {
MOZ_ASSERT(aValue > 999);
MOZ_ASSERT(aValue < 100000);
@ -48,7 +50,8 @@ int32_t ParseStoredValue(int32_t aValue, int32_t& aWindow) {
return score;
}
int32_t PrepareStoredValue(int32_t aScore, int32_t aWindow) {
int32_t BenchmarkStorageParent::PrepareStoredValue(int32_t aScore,
int32_t aWindow) {
MOZ_ASSERT(aScore >= 0);
MOZ_ASSERT(aScore <= 100);
MOZ_ASSERT(aWindow > 0);

Просмотреть файл

@ -28,6 +28,12 @@ class BenchmarkStorageParent : public PBenchmarkStorageParent {
IPCResult RecvCheckVersion(const nsCString& aDbName, int32_t aVersion);
/* Helper methods exposed here to be tested via gtest. */
static void MovingAverage(int32_t& aAverage, int32_t& aWindow,
const int32_t aValue);
static int32_t ParseStoredValue(int32_t aValue, int32_t& aWindow);
static int32_t PrepareStoredValue(int32_t aScore, int32_t aWindow);
private:
RefPtr<KeyValueStorage> mStorage;
};