|
| 1 | +/* globals artifacts */ |
| 2 | + |
| 3 | +const chai = require("chai"); |
| 4 | +const bnChai = require("bn-chai"); |
| 5 | + |
| 6 | +const { INT256_MAX, INT256_MIN, INT128_MIN, INT128_MAX, WAD } = require("../../helpers/constants"); |
| 7 | + |
| 8 | +const { expect } = chai; |
| 9 | +chai.use(bnChai(web3.utils.BN)); |
| 10 | + |
| 11 | +const ScaleReputationTest = artifacts.require("ScaleReputationTest"); |
| 12 | + |
| 13 | +let scaleReputationTest; |
| 14 | + |
| 15 | +contract("ScaleReputation", () => { |
| 16 | + before(async () => { |
| 17 | + scaleReputationTest = await ScaleReputationTest.new(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("when scaling reputation", () => { |
| 21 | + it("should scale reputation up", async () => { |
| 22 | + const scaled = await scaleReputationTest.scaleReputationPublic(100, WAD.muln(2)); |
| 23 | + expect(scaled).to.eq.BN(200); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should scale reputation down", async () => { |
| 27 | + const scaled = await scaleReputationTest.scaleReputationPublic(100, WAD.divn(2)); |
| 28 | + expect(scaled).to.eq.BN(50); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should cap negatively", async () => { |
| 32 | + const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MAX.subn(10), WAD.muln(2)); |
| 33 | + expect(scaled).to.eq.BN(INT128_MAX); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should cap positively", async () => { |
| 37 | + const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MIN.addn(10), WAD.muln(2)); |
| 38 | + expect(scaled).to.eq.BN(INT128_MIN); |
| 39 | + }); |
| 40 | + |
| 41 | + it("deal with calculations that would arithmetically overflow", async () => { |
| 42 | + const scaled = await scaleReputationTest.scaleReputationPublic(INT256_MAX.subn(10), WAD.subn(1)); |
| 43 | + expect(scaled).to.eq.BN(INT128_MAX); |
| 44 | + }); |
| 45 | + |
| 46 | + it("deal with calculations that would arithmetically underflow", async () => { |
| 47 | + const scaled = await scaleReputationTest.scaleReputationPublic(INT256_MIN.addn(10), WAD.subn(1)); |
| 48 | + expect(scaled).to.eq.BN(INT128_MIN); |
| 49 | + }); |
| 50 | + |
| 51 | + it("deal with calculations that exceed our reputation cap during calculation, but not once calculation is complete", async () => { |
| 52 | + const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MIN.addn(10), WAD.subn(1)); |
| 53 | + expect(scaled).to.be.gt.BN(INT128_MIN); |
| 54 | + }); |
| 55 | + |
| 56 | + it("deal with calculations that exceed our reputation cap during calculation, but not once calculation is complete", async () => { |
| 57 | + const scaled = await scaleReputationTest.scaleReputationPublic(INT128_MAX.subn(10), WAD.subn(1)); |
| 58 | + expect(scaled).to.be.lt.BN(INT128_MAX); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments