mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-28 18:30:06 +00:00
Binary search improvements
Modified the algorithm so that the index is either the location of the element if found or the index at which to insert the element to maintain sorted order. Also added some tests to verify the above claim.
This commit is contained in:
+23
-3
@@ -1,9 +1,26 @@
|
||||
ODIN=../../odin
|
||||
PYTHON=$(shell which python3)
|
||||
|
||||
all: download_test_assets image_test compress_test strings_test hash_test crypto_test noise_test encoding_test \
|
||||
math_test linalg_glsl_math_test filepath_test reflect_test os_exit_test i18n_test match_test c_libc_test net_test \
|
||||
fmt_test thread_test
|
||||
all: c_libc_test \
|
||||
compress_test \
|
||||
crypto_test \
|
||||
download_test_assets \
|
||||
encoding_test \
|
||||
filepath_test \
|
||||
fmt_test \
|
||||
hash_test \
|
||||
i18n_test \
|
||||
image_test \
|
||||
linalg_glsl_math_test \
|
||||
match_test \
|
||||
math_test \
|
||||
net_test \
|
||||
noise_test \
|
||||
os_exit_test \
|
||||
reflect_test \
|
||||
slice_test \
|
||||
strings_test \
|
||||
thread_test
|
||||
|
||||
download_test_assets:
|
||||
$(PYTHON) download_assets.py
|
||||
@@ -44,6 +61,9 @@ filepath_test:
|
||||
reflect_test:
|
||||
$(ODIN) run reflect/test_core_reflect.odin -file -collection:tests=.. -out:test_core_reflect
|
||||
|
||||
slice_test:
|
||||
$(ODIN) run slice/test_core_slice.odin -file -out:test_core_slice
|
||||
|
||||
os_exit_test:
|
||||
$(ODIN) run os/test_core_os_exit.odin -file -out:test_core_os_exit && exit 1 || exit 0
|
||||
|
||||
|
||||
@@ -66,6 +66,11 @@ echo Running core:reflect tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run reflect %COMMON% %COLLECTION% -out:test_core_reflect.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:slice tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run slice %COMMON% -out:test_core_slice.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:text/i18n tests
|
||||
echo ---
|
||||
|
||||
@@ -30,6 +30,7 @@ when ODIN_TEST {
|
||||
main :: proc() {
|
||||
t := testing.T{}
|
||||
test_sort_with_indices(&t)
|
||||
test_binary_search(&t)
|
||||
|
||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
||||
if TEST_fail > 0 {
|
||||
@@ -180,3 +181,59 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@test
|
||||
test_binary_search :: proc(t: ^testing.T) {
|
||||
index: int
|
||||
found: bool
|
||||
|
||||
test_search :: proc(s: []i32, v: i32) -> (int, bool) {
|
||||
fmt.printf("Searching for %v in %v\n", v, s)
|
||||
index, found := slice.binary_search(s, v)
|
||||
fmt.printf("index: %v\nfound: %v\n", index, found)
|
||||
|
||||
return index, found
|
||||
}
|
||||
|
||||
s := []i32{0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
|
||||
|
||||
index, found = test_search(s, 13)
|
||||
assert(index == 9, "Expected index to be 9.")
|
||||
assert(found == true, "Expected found to be true.")
|
||||
|
||||
index, found = test_search(s, 4)
|
||||
assert(index == 7, "Expected index to be 7.")
|
||||
assert(found == false, "Expected found to be false.")
|
||||
|
||||
index, found = test_search(s, 100)
|
||||
assert(index == 13, "Expected index to be 13.")
|
||||
assert(found == false, "Expected found to be false.")
|
||||
|
||||
index, found = test_search(s, 1)
|
||||
assert(index >= 1 && index <= 4, "Expected index to be 1, 2, 3, or 4.")
|
||||
assert(found == true, "Expected found to be true.")
|
||||
|
||||
index, found = test_search(s, -1)
|
||||
assert(index == 0, "Expected index to be 0.")
|
||||
assert(found == false, "Expected found to be false.")
|
||||
|
||||
a := []i32{}
|
||||
|
||||
index, found = test_search(a, 13)
|
||||
assert(index == 0, "Expected index to be 0.")
|
||||
assert(found == false, "Expected found to be false.")
|
||||
|
||||
b := []i32{1}
|
||||
|
||||
index, found = test_search(b, 13)
|
||||
assert(index == 1, "Expected index to be 1.")
|
||||
assert(found == false, "Expected found to be false.")
|
||||
|
||||
index, found = test_search(b, 1)
|
||||
assert(index == 0, "Expected index to be 0.")
|
||||
assert(found == true, "Expected found to be true.")
|
||||
|
||||
index, found = test_search(b, 0)
|
||||
assert(index == 0, "Expected index to be 0.")
|
||||
assert(found == false, "Expected found to be false.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user