Explorar el Código

add test to Dichotomie

Olivier Marty hace 8 años
padre
commit
34fc027cf5
Se han modificado 2 ficheros con 16 adiciones y 5 borrados
  1. 5 5
      code/Dichotomie.cpp
  2. 11 0
      code/Dichotomie_test.cpp

+ 5 - 5
code/Dichotomie.cpp

@@ -1,12 +1,12 @@
-// exemple de dichotomie
-void dicho() {
+// example of binary search
+int dicho(int pos, int size) {
   int l = 0, r = size-1;
   while(l < r) {
     int m = l+(r-l)/2;
-    if(position <= m)
+    if(pos <= m) // replace with predicate
 	  r = m;
     else
 	  l = m+1;
   }
-  // position : l
-}
+  return l;
+}

+ 11 - 0
code/Dichotomie_test.cpp

@@ -0,0 +1,11 @@
+#include <bits/stdc++.h>
+using namespace std;
+
+#include "Dichotomie.cpp"
+
+int main() {
+  for(int s = 0; s < 100; s++)
+    for(int i = 0; i < s; i++)
+      assert(dicho(i, s) == i);
+  return 0;
+}