Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-Contribute list percentages now do not underflow #2187

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -96,4 +114,37 @@ TEST_F(BatPublishersTest, concaveScore) {
EXPECT_NEAR(publishers->concaveScore(500000), 74.7025, 0.001f);
}

TEST_F(BatPublishersTest, synopsisNormalizerInternal) {
std::unique_ptr<braveledger_bat_publishers::BatPublishers> bat_publishers =
std::make_unique<braveledger_bat_publishers::BatPublishers>(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