Functional Weave
Code in Rust

finance.margin@1.0.0

vectors.json

4,648 bytes · view raw

[
  { "name": "buy at 60.00, sell at 100.00: a 40 percent margin is a 66.67 percent markup", "args": [{ "minor": 6000, "currency": "GBP" }, { "minor": 10000, "currency": "GBP" }], "expect": { "cost": { "minor": 6000, "currency": "GBP" }, "price": { "minor": 10000, "currency": "GBP" }, "profit": { "minor": 4000, "currency": "GBP" }, "marginBasisPoints": 4000, "markupBasisPoints": 6667 } },
  { "name": "the classic confusion: 50 percent markup is only a 33.33 percent margin", "args": [{ "minor": 10000, "currency": "GBP" }, { "minor": 15000, "currency": "GBP" }], "expect": { "cost": { "minor": 10000, "currency": "GBP" }, "price": { "minor": 15000, "currency": "GBP" }, "profit": { "minor": 5000, "currency": "GBP" }, "marginBasisPoints": 3333, "markupBasisPoints": 5000 } },
  { "name": "double the cost is a 50 percent margin and a 100 percent markup", "args": [{ "minor": 5000, "currency": "GBP" }, { "minor": 10000, "currency": "GBP" }], "expect": { "cost": { "minor": 5000, "currency": "GBP" }, "price": { "minor": 10000, "currency": "GBP" }, "profit": { "minor": 5000, "currency": "GBP" }, "marginBasisPoints": 5000, "markupBasisPoints": 10000 } },
  { "name": "6.99 to 9.99 divides evenly into nothing and both ratios still land on a whole basis point", "args": [{ "minor": 699, "currency": "GBP" }, { "minor": 999, "currency": "GBP" }], "expect": { "cost": { "minor": 699, "currency": "GBP" }, "price": { "minor": 999, "currency": "GBP" }, "profit": { "minor": 300, "currency": "GBP" }, "marginBasisPoints": 3003, "markupBasisPoints": 4292 } },
  { "name": "an exact half basis point rounds up, away from zero", "args": [{ "minor": 31, "currency": "GBP" }, { "minor": 32, "currency": "GBP" }], "expect": { "cost": { "minor": 31, "currency": "GBP" }, "price": { "minor": 32, "currency": "GBP" }, "profit": { "minor": 1, "currency": "GBP" }, "marginBasisPoints": 313, "markupBasisPoints": 323 } },
  { "name": "a loss rounds symmetrically, also away from zero", "args": [{ "minor": 32, "currency": "GBP" }, { "minor": 31, "currency": "GBP" }], "expect": { "cost": { "minor": 32, "currency": "GBP" }, "price": { "minor": 31, "currency": "GBP" }, "profit": { "minor": -1, "currency": "GBP" }, "marginBasisPoints": -323, "markupBasisPoints": -313 } },
  { "name": "selling below cost gives a negative margin, not an error", "args": [{ "minor": 12000, "currency": "GBP" }, { "minor": 10000, "currency": "GBP" }], "expect": { "cost": { "minor": 12000, "currency": "GBP" }, "price": { "minor": 10000, "currency": "GBP" }, "profit": { "minor": -2000, "currency": "GBP" }, "marginBasisPoints": -2000, "markupBasisPoints": -1667 } },
  { "name": "selling at cost is zero margin and zero markup", "args": [{ "minor": 5000, "currency": "GBP" }, { "minor": 5000, "currency": "GBP" }], "expect": { "cost": { "minor": 5000, "currency": "GBP" }, "price": { "minor": 5000, "currency": "GBP" }, "profit": { "minor": 0, "currency": "GBP" }, "marginBasisPoints": 0, "markupBasisPoints": 0 } },
  { "name": "a free sample has a 100 percent margin and no meaningful markup", "args": [{ "minor": 0, "currency": "GBP" }, { "minor": 999, "currency": "GBP" }], "expect": { "cost": { "minor": 0, "currency": "GBP" }, "price": { "minor": 999, "currency": "GBP" }, "profit": { "minor": 999, "currency": "GBP" }, "marginBasisPoints": 10000, "markupBasisPoints": null } },
  { "name": "a giveaway has no meaningful margin but a minus 100 percent markup", "args": [{ "minor": 500, "currency": "GBP" }, { "minor": 0, "currency": "GBP" }], "expect": { "cost": { "minor": 500, "currency": "GBP" }, "price": { "minor": 0, "currency": "GBP" }, "profit": { "minor": -500, "currency": "GBP" }, "marginBasisPoints": null, "markupBasisPoints": -10000 } },
  { "name": "nothing for nothing has neither ratio and no profit", "args": [{ "minor": 0, "currency": "GBP" }, { "minor": 0, "currency": "GBP" }], "expect": { "cost": { "minor": 0, "currency": "GBP" }, "price": { "minor": 0, "currency": "GBP" }, "profit": { "minor": 0, "currency": "GBP" }, "marginBasisPoints": null, "markupBasisPoints": null } },
  { "name": "yen has no minor unit and still works, one is one", "args": [{ "minor": 800, "currency": "JPY" }, { "minor": 1000, "currency": "JPY" }], "expect": { "cost": { "minor": 800, "currency": "JPY" }, "price": { "minor": 1000, "currency": "JPY" }, "profit": { "minor": 200, "currency": "JPY" }, "marginBasisPoints": 2000, "markupBasisPoints": 2500 } },
  { "name": "costing in one currency and pricing in another is an error, not a number", "args": [{ "minor": 6000, "currency": "GBP" }, { "minor": 10000, "currency": "EUR" }], "expectError": "currency mismatch" }
]