[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add unittests for finding the third quartile of a set.



commit 01800ea1e4e0312c8b204541a8e43d7aad67ad61
Author: George Kadianakis <desnacked@xxxxxxxxxx>
Date:   Thu Jul 24 14:31:49 2014 +0300

    Add unittests for finding the third quartile of a set.
---
 src/common/container.h     |    6 ++++++
 src/or/dirserv.c           |    3 ++-
 src/test/test_containers.c |   23 +++++++++++++++++++++++
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/common/container.h b/src/common/container.h
index 26ac851..c3756c8 100644
--- a/src/common/container.h
+++ b/src/common/container.h
@@ -689,5 +689,11 @@ median_int32(int32_t *array, int n_elements)
   return find_nth_int32(array, n_elements, (n_elements-1)/2);
 }
 
+static INLINE uint32_t
+third_quartile_uint32(uint32_t *array, int n_elements)
+{
+  return find_nth_uint32(array, n_elements, (n_elements*3)/4);
+}
+
 #endif
 
diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 1139dc1..a5ad742 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -1420,7 +1420,8 @@ dirserv_compute_performance_thresholds(routerlist_t *rl,
     /* (Now bandwidths is sorted.) */
     if (fast_bandwidth_kb < ROUTER_REQUIRED_MIN_BANDWIDTH/(2 * 1000))
       fast_bandwidth_kb = bandwidths_kb[n_active/4];
-    guard_bandwidth_including_exits_kb = bandwidths_kb[n_active*3/4];
+    guard_bandwidth_including_exits_kb =
+      third_quartile_uint32(bandwidths_kb, n_active);
     guard_tk = find_nth_long(tks, n_active, n_active/8);
   }
 
diff --git a/src/test/test_containers.c b/src/test/test_containers.c
index a9f5e72..d7b7b3c 100644
--- a/src/test/test_containers.c
+++ b/src/test/test_containers.c
@@ -835,6 +835,7 @@ static void
 test_container_order_functions(void)
 {
   int lst[25], n = 0;
+  unsigned int lst2[25];
   //  int a=12,b=24,c=25,d=60,e=77;
 
 #define median() median_int(lst, n)
@@ -856,6 +857,28 @@ test_container_order_functions(void)
   test_eq(25, median()); /* 12,12,24,25,60,77,77 */
 #undef median
 
+#define third_quartile() third_quartile_uint32(lst2, n)
+
+  n = 0;
+  lst2[n++] = 1;
+  test_eq(1, third_quartile()); /* ~1~ */
+  lst2[n++] = 2;
+  test_eq(2, third_quartile()); /* 1, ~2~ */
+  lst2[n++] = 3;
+  lst2[n++] = 4;
+  lst2[n++] = 5;
+  test_eq(4, third_quartile()); /* 1, 2, 3, ~4~, 5 */
+  lst2[n++] = 6;
+  lst2[n++] = 7;
+  lst2[n++] = 8;
+  lst2[n++] = 9;
+  test_eq(7, third_quartile()); /* 1, 2, 3, 4, 5, 6, ~7~, 8, 9 */
+  lst2[n++] = 10;
+  lst2[n++] = 11;
+  test_eq(9, third_quartile()); /* 1, 2, 3, 4, 5, 6, 7, 8, ~9~, 10, 11 */
+
+#undef third_quartile
+
  done:
   ;
 }

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits