Skip to content

Instantly share code, notes, and snippets.

@karmacoma-eth
Created December 2, 2022 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karmacoma-eth/01227912ad012fdd8056dbf7819c4bb2 to your computer and use it in GitHub Desktop.
Save karmacoma-eth/01227912ad012fdd8056dbf7819c4bb2 to your computer and use it in GitHub Desktop.
FizzBuzz in Solidity but ridiculous
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import {console2} from "forge-std/console2.sol";
contract FizzBuzz {
modifier fizz(uint256 n) {
_;
if (n % 3 == 0 && n % 5 != 0) {
console2.log("Fizz");
}
}
modifier buzz(uint256 n) {
_;
if (n % 5 == 0 && n % 3 != 0) {
console2.log("Buzz");
}
}
modifier both(uint256 n) {
_;
if (n % 3 == 0 && n % 5 == 0) {
console2.log("Fizz Buzz");
}
}
modifier neither(uint256 n) {
_;
if (n % 3 != 0 && n % 5 != 0) {
console2.log(n);
}
}
modifier recurse(uint256 n) {
_;
if (n > 1) {
fizzbuzz(n - 1);
}
}
function fizzbuzz(uint256 n) public fizz(n) buzz(n) both(n) neither(n) recurse(n) {}
}
contract FizzBuzzTest {
FizzBuzz fizzbuzz;
function setUp() public {
fizzbuzz = new FizzBuzz();
}
function testFizzbuzz100() public {
fizzbuzz.fizzbuzz(100);
}
}
@karmacoma-eth
Copy link
Author

$ forge test --match testFizz -vv
...
[PASS] testFizzbuzz100() (gas: 120866)
Logs:
  1
  2
  Fizz
  4
  Buzz
  Fizz
  7
  8
  Fizz
  Buzz
  11
  Fizz
  13
  14
  Fizz Buzz
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment