From 8035f80f5407058ab69f4a48610478755c718ee3 Mon Sep 17 00:00:00 2001 From: James Schloss Date: Wed, 3 Nov 2021 16:48:26 +0100 Subject: [PATCH 1/2] attempt at output standardization for approximate counting --- .../code/c/approximate_counting.c | 13 +++++++--- .../code/cpp/approximate_counting.cpp | 8 +++--- .../code/julia/approximate_counting.jl | 26 ++++++++++++------- .../code/python/approximate_counting.py | 9 ++++--- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/contents/approximate_counting/code/c/approximate_counting.c b/contents/approximate_counting/code/c/approximate_counting.c index da44334a7..c5034f49e 100644 --- a/contents/approximate_counting/code/c/approximate_counting.c +++ b/contents/approximate_counting/code/c/approximate_counting.c @@ -63,7 +63,12 @@ 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() @@ -71,11 +76,11 @@ int main() srand(time(NULL)); printf("Counting Tests, 100 trials\n"); - printf("testing 1000, a = 30, 10%% error\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..53f5317c5 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 << "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..4075d8956 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("Counting 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..76e7618b6 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("Counting Tests, 100 trials\n") +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) From 096a77a47c4a55c081bdac3234f9d8fd58a99c07 Mon Sep 17 00:00:00 2001 From: James Schloss Date: Mon, 8 Nov 2021 09:14:01 +0100 Subject: [PATCH 2/2] further standardization --- contents/approximate_counting/code/c/approximate_counting.c | 2 +- contents/approximate_counting/code/cpp/approximate_counting.cpp | 2 +- .../approximate_counting/code/julia/approximate_counting.jl | 2 +- .../approximate_counting/code/python/approximate_counting.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/approximate_counting/code/c/approximate_counting.c b/contents/approximate_counting/code/c/approximate_counting.c index c5034f49e..2be6ffaf8 100644 --- a/contents/approximate_counting/code/c/approximate_counting.c +++ b/contents/approximate_counting/code/c/approximate_counting.c @@ -75,7 +75,7 @@ int main() { srand(time(NULL)); - printf("Counting Tests, 100 trials\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("[#]\ntesting 12,345, a = 10, 10%% error\n"); diff --git a/contents/approximate_counting/code/cpp/approximate_counting.cpp b/contents/approximate_counting/code/cpp/approximate_counting.cpp index 53f5317c5..1ee2790b7 100644 --- a/contents/approximate_counting/code/cpp/approximate_counting.cpp +++ b/contents/approximate_counting/code/cpp/approximate_counting.cpp @@ -59,7 +59,7 @@ auto test_approximate_count( } int main() { - std::cout << "Counting Tests, 100 trials\n"; + std::cout << "[#]\nCounting Tests, 100 trials\n"; std::cout << "[#]\ntesting 1,000, a = 30, 10% error \n" << test_approximate_count(100, 1000, 30, 0.1) << "\n"; diff --git a/contents/approximate_counting/code/julia/approximate_counting.jl b/contents/approximate_counting/code/julia/approximate_counting.jl index 4075d8956..24c9f0fb6 100644 --- a/contents/approximate_counting/code/julia/approximate_counting.jl +++ b/contents/approximate_counting/code/julia/approximate_counting.jl @@ -54,7 +54,7 @@ function test_approximate_count(n_trials, n_items, a, threshold) end end -println("Counting Tests, 100 trials") +println("[#]\nCounting Tests, 100 trials") println("[#]\ntesting 1,000, a = 30, 10% error") test_approximate_count(100, 1000, 30, 0.1) diff --git a/contents/approximate_counting/code/python/approximate_counting.py b/contents/approximate_counting/code/python/approximate_counting.py index 76e7618b6..a8381ffe8 100644 --- a/contents/approximate_counting/code/python/approximate_counting.py +++ b/contents/approximate_counting/code/python/approximate_counting.py @@ -43,7 +43,7 @@ def test_approximate_count(n_trials, n_items, a, threshold): else: print("failed") -print("Counting Tests, 100 trials\n") +print("[#]\nCounting Tests, 100 trials") print("[#]\ntesting 1,000, a = 30, 10% error") test_approximate_count(100, 1000, 30, 0.1) print("[#]\ntesting 12,345, a = 10, 10% error")