Functional Weave
Code in Rust

money.split-even@1.0.0

vectors.json

4,566 bytes · view raw

[
  {
    "name": "ten pounds three ways keeps the penny, larger share first",
    "args": [
      {
        "minor": 1000,
        "currency": "GBP"
      },
      3
    ],
    "expect": [
      {
        "minor": 334,
        "currency": "GBP"
      },
      {
        "minor": 333,
        "currency": "GBP"
      },
      {
        "minor": 333,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "a refund splits as the mirror image, unlike allocate's -333, -333, -334",
    "args": [
      {
        "minor": -1000,
        "currency": "GBP"
      },
      3
    ],
    "expect": [
      {
        "minor": -334,
        "currency": "GBP"
      },
      {
        "minor": -333,
        "currency": "GBP"
      },
      {
        "minor": -333,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "an exact split has no leftover",
    "args": [
      {
        "minor": 1000,
        "currency": "GBP"
      },
      4
    ],
    "expect": [
      {
        "minor": 250,
        "currency": "GBP"
      },
      {
        "minor": 250,
        "currency": "GBP"
      },
      {
        "minor": 250,
        "currency": "GBP"
      },
      {
        "minor": 250,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "two leftover pennies go to the first two parts",
    "args": [
      {
        "minor": 10000,
        "currency": "GBP"
      },
      6
    ],
    "expect": [
      {
        "minor": 1667,
        "currency": "GBP"
      },
      {
        "minor": 1667,
        "currency": "GBP"
      },
      {
        "minor": 1667,
        "currency": "GBP"
      },
      {
        "minor": 1667,
        "currency": "GBP"
      },
      {
        "minor": 1666,
        "currency": "GBP"
      },
      {
        "minor": 1666,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "fewer pennies than parts leaves some parts at zero",
    "args": [
      {
        "minor": 2,
        "currency": "GBP"
      },
      3
    ],
    "expect": [
      {
        "minor": 1,
        "currency": "GBP"
      },
      {
        "minor": 1,
        "currency": "GBP"
      },
      {
        "minor": 0,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "a negative amount smaller than the parts",
    "args": [
      {
        "minor": -2,
        "currency": "GBP"
      },
      5
    ],
    "expect": [
      {
        "minor": -1,
        "currency": "GBP"
      },
      {
        "minor": -1,
        "currency": "GBP"
      },
      {
        "minor": 0,
        "currency": "GBP"
      },
      {
        "minor": 0,
        "currency": "GBP"
      },
      {
        "minor": 0,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "zero splits into zeros",
    "args": [
      {
        "minor": 0,
        "currency": "GBP"
      },
      3
    ],
    "expect": [
      {
        "minor": 0,
        "currency": "GBP"
      },
      {
        "minor": 0,
        "currency": "GBP"
      },
      {
        "minor": 0,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "one part is the whole amount",
    "args": [
      {
        "minor": 999,
        "currency": "GBP"
      },
      1
    ],
    "expect": [
      {
        "minor": 999,
        "currency": "GBP"
      }
    ]
  },
  {
    "name": "yen splits in whole yen",
    "args": [
      {
        "minor": 100,
        "currency": "JPY"
      },
      3
    ],
    "expect": [
      {
        "minor": 34,
        "currency": "JPY"
      },
      {
        "minor": 33,
        "currency": "JPY"
      },
      {
        "minor": 33,
        "currency": "JPY"
      }
    ]
  },
  {
    "name": "dinar splits in fils",
    "args": [
      {
        "minor": 1000,
        "currency": "KWD"
      },
      3
    ],
    "expect": [
      {
        "minor": 334,
        "currency": "KWD"
      },
      {
        "minor": 333,
        "currency": "KWD"
      },
      {
        "minor": 333,
        "currency": "KWD"
      }
    ]
  },
  {
    "name": "zero parts is an error",
    "args": [
      {
        "minor": 100,
        "currency": "GBP"
      },
      0
    ],
    "expectError": "parts must be at least 1"
  },
  {
    "name": "negative parts is an error",
    "args": [
      {
        "minor": 100,
        "currency": "GBP"
      },
      -2
    ],
    "expectError": "parts must be at least 1"
  },
  {
    "name": "fractional parts is an error",
    "args": [
      {
        "minor": 100,
        "currency": "GBP"
      },
      2.5
    ],
    "expectError": "parts must be a whole number"
  }
]