From 4f70734b5a6c56ba3df3f8381047cdd045440bf9 Mon Sep 17 00:00:00 2001 From: Jason Sadler Date: Tue, 9 Apr 2019 17:49:22 -0400 Subject: [PATCH] Fixes brave/brave-browser#3668 Address PR comments --- .../src/bat/ledger/internal/bat_publishers.cc | 12 +++-- .../src/bat/ledger/internal/bat_publishers.h | 1 + .../internal/bat_publishers_unittest.cc | 51 +++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc index ebf2cab65ded..76aff303e5b9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc @@ -461,11 +461,15 @@ void BatPublishers::synopsisNormalizerInternal( } if (percents.size() != 0) { if (totalPercents > 100) { - percents[valueToChange] -= 1; - totalPercents -= 1; + if (percents[valueToChange] != 0) { + percents[valueToChange] -= 1; + totalPercents -= 1; + } } else { - percents[valueToChange] += 1; - totalPercents += 1; + if (percents[valueToChange] != 100) { + percents[valueToChange] += 1; + totalPercents += 1; + } } roundoffs[valueToChange] = 0; } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h index 4815edb78183..fda4aa1cdaca 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h @@ -218,6 +218,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { friend class BatPublishersTest; FRIEND_TEST_ALL_PREFIXES(BatPublishersTest, calcScoreConsts); FRIEND_TEST_ALL_PREFIXES(BatPublishersTest, concaveScore); + FRIEND_TEST_ALL_PREFIXES(BatPublishersTest, synopsisNormalizerInternal); }; } // namespace braveledger_bat_publishers diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc index 99455575aae6..0c270860bef5 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc @@ -12,6 +12,24 @@ namespace braveledger_bat_publishers { class BatPublishersTest : public testing::Test { + protected: + void CreatePublisherInfoList( + ledger::PublisherInfoList* list) { + double prev_score; + for (int ix = 0; ix < 50; ix++) { + ledger::PublisherInfo info("example" + std::to_string(ix) + ".com"); + info.duration = 50; + if (ix == 0) { + info.score = 24; + } else { + info.score = prev_score / 2; + } + prev_score = info.score; + info.reconcile_stamp = 0; + info.visits = 5; + list->push_back(info); + } + } }; TEST_F(BatPublishersTest, calcScoreConsts) { @@ -96,4 +114,37 @@ TEST_F(BatPublishersTest, concaveScore) { EXPECT_NEAR(publishers->concaveScore(500000), 74.7025, 0.001f); } +TEST_F(BatPublishersTest, synopsisNormalizerInternal) { + std::unique_ptr bat_publishers = + std::make_unique(nullptr); + // create test PublisherInfo list + ledger::PublisherInfoList new_list; + ledger::PublisherInfoList list; + CreatePublisherInfoList(&list); + bat_publishers->synopsisNormalizerInternal( + &new_list, list, 0); + + // simulate exclude and re-normalize + new_list.erase(new_list.begin() + 3); + ledger::PublisherInfoList new_list2; + bat_publishers->synopsisNormalizerInternal( + &new_list2, new_list, 0); + new_list2.erase(new_list2.begin() + 4); + ledger::PublisherInfoList new_list3; + bat_publishers->synopsisNormalizerInternal( + &new_list3, new_list2, 0); + new_list3.erase(new_list3.begin() + 5); + ledger::PublisherInfoList new_list4; + bat_publishers->synopsisNormalizerInternal( + &new_list4, new_list3, 0); + new_list4.erase(new_list4.begin() + 6); + ledger::PublisherInfoList new_list5; + bat_publishers->synopsisNormalizerInternal( + &new_list5, new_list4, 0); + for (auto element : new_list5) { + ASSERT_GE((int32_t)element.percent, 0); + ASSERT_LE((int32_t)element.percent, 100); + } +} + } // namespace braveledger_bat_publishers