From edbf50bfaeba82f84aa14f51cd26ef0d29dbd32b Mon Sep 17 00:00:00 2001 From: James Schloss Date: Mon, 22 Nov 2021 19:16:35 +0100 Subject: [PATCH] attempt at output standardization for approximate counting (#913) * attempt at output standardization for approximate counting * further standardization --- .../code/c/approximate_counting.c | 15 +++++++---- .../code/cpp/approximate_counting.cpp | 10 +++---- .../code/julia/approximate_counting.jl | 26 ++++++++++++------- .../code/python/approximate_counting.py | 9 ++++--- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/contents/approximate_counting/code/c/approximate_counting.c b/contents/approximate_counting/code/c/approximate_counting.c index da44334a7..2be6ffaf8 100644 --- a/contents/approximate_counting/code/c/approximate_counting.c +++ b/contents/approximate_counting/code/c/approximate_counting.c @@ -63,19 +63,24 @@ void test_approximation_count(size_t n_trials, size_t n_items, double a, } double avg = sum / n_trials; - assert(fabs((avg - n_items) / n_items) < threshold); + if (fabs((avg - n_items) / n_items) < threshold){ + printf("passed\n"); + } + else{ + printf("failed\n"); + } } int main() { srand(time(NULL)); - printf("Counting Tests, 100 trials\n"); - printf("testing 1000, a = 30, 10%% error\n"); + printf("[#]\nCounting Tests, 100 trials\n"); + printf("[#]\ntesting 1,000, a = 30, 10%% error\n"); test_approximation_count(100, 1000, 30, 0.1); - printf("testing 12345, a = 10, 10%% error\n"); + printf("[#]\ntesting 12,345, a = 10, 10%% error\n"); test_approximation_count(100, 12345, 10, 0.1); - printf("testing 222222, a = 0.5, 20%% error\n"); + printf("[#]\ntesting 222,222, a = 0.5, 20%% error\n"); test_approximation_count(100, 222222, 0.5, 0.2); return 0; diff --git a/contents/approximate_counting/code/cpp/approximate_counting.cpp b/contents/approximate_counting/code/cpp/approximate_counting.cpp index 53f4641af..1ee2790b7 100644 --- a/contents/approximate_counting/code/cpp/approximate_counting.cpp +++ b/contents/approximate_counting/code/cpp/approximate_counting.cpp @@ -55,17 +55,17 @@ auto test_approximate_count( for (auto i = 0; i < n_trials; ++i) sum += approximate_count(n_items, a); const auto avg = sum / n_trials; - return std::abs((avg - n_items) / n_items) < threshold ? "pass" : "fail"; + return std::abs((avg - n_items) / n_items) < threshold ? "passed" : "failed"; } int main() { - std::cout << "Counting Tests, 100 trials\n"; + std::cout << "[#]\nCounting Tests, 100 trials\n"; - std::cout << "testing 1,000, a = 30, 10% error " + std::cout << "[#]\ntesting 1,000, a = 30, 10% error \n" << test_approximate_count(100, 1000, 30, 0.1) << "\n"; - std::cout << "testing 12,345, a = 10, 10% error " + std::cout << "[#]\ntesting 12,345, a = 10, 10% error \n" << test_approximate_count(100, 12345, 10, 0.1) << "\n"; // Note : with a lower a, we need more trials, so a higher % error here. - std::cout << "testing 222,222, a = 0.5, 20% error " + std::cout << "[#]\ntesting 222,222, a = 0.5, 20% error \n" << test_approximate_count(100, 222222, 0.5, 0.2) << "\n"; } diff --git a/contents/approximate_counting/code/julia/approximate_counting.jl b/contents/approximate_counting/code/julia/approximate_counting.jl index c6cf3b223..24c9f0fb6 100644 --- a/contents/approximate_counting/code/julia/approximate_counting.jl +++ b/contents/approximate_counting/code/julia/approximate_counting.jl @@ -47,15 +47,21 @@ function test_approximate_count(n_trials, n_items, a, threshold) avg = sum(samples)/n_trials - @test (abs((avg - n_items) / n_items) < threshold) + if (abs((avg - n_items) / n_items) < threshold) + println("passed") + else + println("failed") + end end -@testset "Counting Tests, 100 trials" begin - println("testing 1,000, a = 30, 10% error") - test_approximate_count(100, 1000, 30, 0.1) - println("testing 12,345, a = 10, 10% error") - test_approximate_count(100, 12345, 10, 0.1) - # Note: with a lower a, we need more trials, so a higher % error here. - println("testing 222,222, a = 0.5, 20% error") - test_approximate_count(100, 222222, 0.5, 0.2) -end +println("[#]\nCounting Tests, 100 trials") + +println("[#]\ntesting 1,000, a = 30, 10% error") +test_approximate_count(100, 1000, 30, 0.1) + +println("[#]\ntesting 12,345, a = 10, 10% error") +test_approximate_count(100, 12345, 10, 0.1) + +# Note: with a lower a, we need more trials, so a higher % error here. +println("[#]\ntesting 222,222, a = 0.5, 20% error") +test_approximate_count(100, 222222, 0.5, 0.2) diff --git a/contents/approximate_counting/code/python/approximate_counting.py b/contents/approximate_counting/code/python/approximate_counting.py index 0088debcc..a8381ffe8 100644 --- a/contents/approximate_counting/code/python/approximate_counting.py +++ b/contents/approximate_counting/code/python/approximate_counting.py @@ -40,10 +40,13 @@ def test_approximate_count(n_trials, n_items, a, threshold): if abs((avg - n_items)/n_items) < threshold: print("passed") + else: + print("failed") -print("testing 1,000, a = 30, 10% error") +print("[#]\nCounting Tests, 100 trials") +print("[#]\ntesting 1,000, a = 30, 10% error") test_approximate_count(100, 1000, 30, 0.1) -print("testing 12,345, a = 10, 10% error") +print("[#]\ntesting 12,345, a = 10, 10% error") test_approximate_count(100, 12345, 10, 0.1) -print("testing 222,222, a = 0.5, 20% error") +print("[#]\ntesting 222,222, a = 0.5, 20% error") test_approximate_count(100, 222222, 0.5, 0.2)