From bf0f36d5a9fb4a9595b1ff85dca036b1e9f65a6c Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 20 Feb 2014 08:12:02 +0900 Subject: [PATCH] rewrite example/fizzbuzz --- example/fizzbuzz/main.cpp | 57 +++++++++++++++------------------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/example/fizzbuzz/main.cpp b/example/fizzbuzz/main.cpp index ee4b219d..00392955 100644 --- a/example/fizzbuzz/main.cpp +++ b/example/fizzbuzz/main.cpp @@ -12,39 +12,40 @@ // Boost Software License - Version 1.0 // // -#include -#include #include -#include -#include - struct fizzbuzz{ typedef sprout::string<12> result_type; constexpr result_type operator()(int n) const { - return n % 15 == 0 ? sprout::to_string("FizzBuzz") - : n % 3 == 0 ? sprout::to_string("Fizz") - : n % 5 == 0 ? sprout::to_string("Buzz") + return n % 15 == 0 ? "FizzBuzz" + : n % 3 == 0 ? "Fizz" + : n % 5 == 0 ? "Buzz" : sprout::to_string(n); } }; +// +// Test +// +static_assert(fizzbuzz()( 1) == "1", ""); +static_assert(fizzbuzz()( 2) == "2", ""); +static_assert(fizzbuzz()( 3) == "Fizz", ""); +static_assert(fizzbuzz()( 5) == "Buzz", ""); +static_assert(fizzbuzz()(15) == "FizzBuzz", ""); + + + +#include +#include +#include +#include int main(){ typedef fizzbuzz::result_type string; - // - // Test - // - static_assert(fizzbuzz()( 1) == "1", ""); - static_assert(fizzbuzz()( 2) == "2", ""); - static_assert(fizzbuzz()( 3) == "Fizz", ""); - static_assert(fizzbuzz()( 5) == "Buzz", ""); - static_assert(fizzbuzz()(15) == "FizzBuzz", ""); - // // Sequence [1..15] // @@ -63,21 +64,9 @@ main(){ // Check result // constexpr auto fizzbuzz_result = sprout::make_array( - sprout::to_string("1"), - sprout::to_string("2"), - sprout::to_string("Fizz"), - sprout::to_string("4"), - sprout::to_string("Buzz"), - sprout::to_string("Fizz"), - sprout::to_string("7"), - sprout::to_string("8"), - sprout::to_string("Fizz"), - sprout::to_string("Buzz"), - sprout::to_string("11"), - sprout::to_string("Fizz"), - sprout::to_string("13"), - sprout::to_string("14"), - sprout::to_string("FizzBuzz") + "1", "2", "Fizz", "4", "Buzz", + "Fizz", "7", "8", "Fizz", "Buzz", + "11", "Fizz", "13", "14", "FizzBuzz" ); // Equal result sequence static_assert(result == fizzbuzz_result, ""); @@ -85,10 +74,8 @@ main(){ // // Output // - for (auto&& str : result){ + for (auto&& str : result) { std::cout << str << ", "; } std::cout << std::endl; - - return 0; }