From 6cb9b331e3ad682b398475442d27e71b21ea3b95 Mon Sep 17 00:00:00 2001 From: Ramy Date: Wed, 25 Sep 2024 00:16:11 +0200 Subject: [PATCH 01/23] Create blank.yml --- .github/workflows/blank.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/blank.yml diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml new file mode 100644 index 0000000..4816668 --- /dev/null +++ b/.github/workflows/blank.yml @@ -0,0 +1,19 @@ +name: Code Style + +on: [push, pull_request] +permissions: + contents: read +jobs: + fpm: + name: Fortran Code Formatting + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Install fprettify + run: | + sudo apt-get install -y python3-pip + pip3 install fprettify + - name: Format Fortran files + run: | + fprettify --indent 4 -d --recursive . From cbb37ecb8e3df8a3e561804510c71fb9d643593c Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:30:19 +0200 Subject: [PATCH 02/23] Update blank.yml --- .github/workflows/blank.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 4816668..7f60f22 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -16,4 +16,13 @@ jobs: pip3 install fprettify - name: Format Fortran files run: | - fprettify --indent 4 -d --recursive . + fprettify -i 4 -d --recursive . + - name: Fail if needs reformatting + run: | + if [[ $(git status --porcelain) ]]; then + echo "please reformat/fprettify these files:" + git status --porcelain=v1 + echo "exact diff:" + fprettify -i 4 -r -d . 2>&1 + exit 1 + fi From 4c2571c416436ea1eae07eb380b03270a8d591aa Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:31:36 +0200 Subject: [PATCH 03/23] Update blank.yml --- .github/workflows/blank.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 7f60f22..3d8004c 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -23,6 +23,6 @@ jobs: echo "please reformat/fprettify these files:" git status --porcelain=v1 echo "exact diff:" - fprettify -i 4 -r -d . 2>&1 + fprettify -i 4 -r -d . exit 1 fi From bddd0489560848f93610ea28ed6fcfd06fab460c Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:33:49 +0200 Subject: [PATCH 04/23] Update blank.yml --- .github/workflows/blank.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 3d8004c..57cf225 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -23,6 +23,6 @@ jobs: echo "please reformat/fprettify these files:" git status --porcelain=v1 echo "exact diff:" - fprettify -i 4 -r -d . + git diff exit 1 - fi + fi From 324e2e6b1a2bc095e23d18134ad2cb7477e35717 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:38:07 +0200 Subject: [PATCH 05/23] Update blank.yml --- .github/workflows/blank.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 57cf225..7a31500 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -17,6 +17,7 @@ jobs: - name: Format Fortran files run: | fprettify -i 4 -d --recursive . + git add . - name: Fail if needs reformatting run: | if [[ $(git status --porcelain) ]]; then From c4eb404669eeda1040902559796415fe078f6d53 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:39:17 +0200 Subject: [PATCH 06/23] Update blank.yml --- .github/workflows/blank.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 7a31500..730fffc 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -16,7 +16,7 @@ jobs: pip3 install fprettify - name: Format Fortran files run: | - fprettify -i 4 -d --recursive . + fprettify -i 4 --recursive . git add . - name: Fail if needs reformatting run: | From 31b4b5e5e211e2ea98f551bb3b32bac3c1050528 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:41:57 +0200 Subject: [PATCH 07/23] Update blank.yml --- .github/workflows/blank.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 730fffc..c1b44d9 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -24,6 +24,7 @@ jobs: echo "please reformat/fprettify these files:" git status --porcelain=v1 echo "exact diff:" - git diff + fprettify -i 4 --recursive . + git diff exit 1 fi From aa8954236934dea2498c0fd318db0d2c00357740 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:42:51 +0200 Subject: [PATCH 08/23] Update blank.yml --- .github/workflows/blank.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index c1b44d9..da8f62d 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -24,7 +24,7 @@ jobs: echo "please reformat/fprettify these files:" git status --porcelain=v1 echo "exact diff:" - fprettify -i 4 --recursive . + fprettify -i 4 -d --recursive . git diff exit 1 fi From 53fb8ef90519a28eb004116fd03bd05353a4dc5d Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:49:17 +0200 Subject: [PATCH 09/23] Update blank.yml --- .github/workflows/blank.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index da8f62d..4792479 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -16,15 +16,22 @@ jobs: pip3 install fprettify - name: Format Fortran files run: | - fprettify -i 4 --recursive . + fprettify -i 4 -d --recursive . git add . - name: Fail if needs reformatting run: | + # Check if there are uncommitted changes if [[ $(git status --porcelain) ]]; then echo "please reformat/fprettify these files:" git status --porcelain=v1 echo "exact diff:" - fprettify -i 4 -d --recursive . - git diff + + # Show the formatting changes with fprettify + fprettify -i 4 -d --recursive . # This will display debug output + + # Display the git diff + git diff exit 1 - fi + else + echo "All files are properly formatted." + fi From 61cb114a7cd14ba13b874e474d0e5acd15145b81 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:50:50 +0200 Subject: [PATCH 10/23] Update blank.yml --- .github/workflows/blank.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 4792479..e5ae402 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -17,7 +17,14 @@ jobs: - name: Format Fortran files run: | fprettify -i 4 -d --recursive . - git add . + # Check if files were modified and stage them for commit + git diff --exit-code + if [ $? -ne 0 ]; then + echo "Files were modified by fprettify, adding changes." + git add . + else + echo "No files were modified." + fi - name: Fail if needs reformatting run: | # Check if there are uncommitted changes From 7272baa28a638d3e27f15ad8bf8dd9c265bf7d9f Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:55:20 +0200 Subject: [PATCH 11/23] Update blank.yml --- .github/workflows/blank.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index e5ae402..8f73106 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -16,15 +16,8 @@ jobs: pip3 install fprettify - name: Format Fortran files run: | - fprettify -i 4 -d --recursive . - # Check if files were modified and stage them for commit - git diff --exit-code - if [ $? -ne 0 ]; then - echo "Files were modified by fprettify, adding changes." - git add . - else - echo "No files were modified." - fi + fprettify -i 4 -d --recursive . 2>&1 + git add . - name: Fail if needs reformatting run: | # Check if there are uncommitted changes From cdcbe96040320b449f44e00de246165abbf713bd Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 01:58:04 +0200 Subject: [PATCH 12/23] Update blank.yml --- .github/workflows/blank.yml | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 8f73106..953a771 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -19,19 +19,11 @@ jobs: fprettify -i 4 -d --recursive . 2>&1 git add . - name: Fail if needs reformatting - run: | - # Check if there are uncommitted changes - if [[ $(git status --porcelain) ]]; then + run: | echo "please reformat/fprettify these files:" git status --porcelain=v1 echo "exact diff:" - - # Show the formatting changes with fprettify - fprettify -i 4 -d --recursive . # This will display debug output - - # Display the git diff + + fprettify -i 4 -d --recursive . git diff - exit 1 - else - echo "All files are properly formatted." - fi + exit 1 From ffb7b28cb36ffc772653c23df260f68719303cbb Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:01:00 +0200 Subject: [PATCH 13/23] Update blank.yml --- .github/workflows/blank.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 953a771..c9918e9 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -16,7 +16,7 @@ jobs: pip3 install fprettify - name: Format Fortran files run: | - fprettify -i 4 -d --recursive . 2>&1 + fprettify -i 4 --recursive . git add . - name: Fail if needs reformatting run: | From d2d856190ed49f1057eddf99faf3d460e05f896e Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:02:51 +0200 Subject: [PATCH 14/23] Update blank.yml --- .github/workflows/blank.yml | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index c9918e9..99ba775 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -15,15 +15,17 @@ jobs: sudo apt-get install -y python3-pip pip3 install fprettify - name: Format Fortran files + id: format run: | - fprettify -i 4 --recursive . - git add . - - name: Fail if needs reformatting - run: | - echo "please reformat/fprettify these files:" - git status --porcelain=v1 - echo "exact diff:" - - fprettify -i 4 -d --recursive . - git diff - exit 1 + # Run fprettify and capture output + output=$(fprettify -i 4 --recursive .) + echo "$output" + # Check if there were any changes + if [[ $(git status --porcelain) ]]; then + echo "Files were modified:" + echo "$output" + git diff + exit 1 + else + echo "All files are properly formatted." + fi From f9e59b050180ff9efff0d58cded18e3d476a2784 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:05:57 +0200 Subject: [PATCH 15/23] Update blank.yml --- .github/workflows/blank.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 99ba775..392ceba 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -16,13 +16,12 @@ jobs: pip3 install fprettify - name: Format Fortran files id: format - run: | - # Run fprettify and capture output + run: | output=$(fprettify -i 4 --recursive .) echo "$output" - # Check if there were any changes - if [[ $(git status --porcelain) ]]; then - echo "Files were modified:" + - name: Fail if needs reformatting + if [[ $(git status --porcelain) ]]; then + echo "please reformat/fprettify these files:" echo "$output" git diff exit 1 From 503722c235bc49762fedbec31d78f70e820130f8 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:07:41 +0200 Subject: [PATCH 16/23] Update blank.yml --- .github/workflows/blank.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 392ceba..d58f4cf 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -19,7 +19,8 @@ jobs: run: | output=$(fprettify -i 4 --recursive .) echo "$output" - - name: Fail if needs reformatting + - name: Fail if needs reformatting + run: | if [[ $(git status --porcelain) ]]; then echo "please reformat/fprettify these files:" echo "$output" From 01f802865263bdcaa70778afe6a297010cce129c Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:10:46 +0200 Subject: [PATCH 17/23] Update blank.yml --- .github/workflows/blank.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index d58f4cf..ef79f5e 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -17,14 +17,13 @@ jobs: - name: Format Fortran files id: format run: | - output=$(fprettify -i 4 --recursive .) - echo "$output" + output=$(fprettify -i 4 -d --recursive .) - name: Fail if needs reformatting run: | if [[ $(git status --porcelain) ]]; then echo "please reformat/fprettify these files:" echo "$output" - git diff + #git diff exit 1 else echo "All files are properly formatted." From 5c0013353892e9fadcc66d2137156df83285e057 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:11:23 +0200 Subject: [PATCH 18/23] Update blank.yml --- .github/workflows/blank.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index ef79f5e..0982351 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -17,7 +17,7 @@ jobs: - name: Format Fortran files id: format run: | - output=$(fprettify -i 4 -d --recursive .) + output=$(fprettify -i 4 --recursive .) - name: Fail if needs reformatting run: | if [[ $(git status --porcelain) ]]; then From 18895debec4383c8765505b310faebbe38eaad2c Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:12:01 +0200 Subject: [PATCH 19/23] Update blank.yml --- .github/workflows/blank.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index 0982351..c7df705 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -22,8 +22,8 @@ jobs: run: | if [[ $(git status --porcelain) ]]; then echo "please reformat/fprettify these files:" - echo "$output" - #git diff + #echo "$output" + git diff exit 1 else echo "All files are properly formatted." From ff3d52ed6f7408e307d57e21894423dfa5fe9690 Mon Sep 17 00:00:00 2001 From: Ramy Date: Thu, 26 Sep 2024 02:14:44 +0200 Subject: [PATCH 20/23] Update blank.yml --- .github/workflows/blank.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml index c7df705..c86a241 100644 --- a/.github/workflows/blank.yml +++ b/.github/workflows/blank.yml @@ -17,12 +17,11 @@ jobs: - name: Format Fortran files id: format run: | - output=$(fprettify -i 4 --recursive .) + fprettify -i 4 --recursive . - name: Fail if needs reformatting run: | if [[ $(git status --porcelain) ]]; then - echo "please reformat/fprettify these files:" - #echo "$output" + echo "please reformat/fprettify these files:" git diff exit 1 else From 34b544b994678674f1adc62efb252d6d828c6af2 Mon Sep 17 00:00:00 2001 From: Ramy Date: Mon, 30 Sep 2024 09:38:27 +0200 Subject: [PATCH 21/23] Delete .github/workflows/blank.yml --- .github/workflows/blank.yml | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 .github/workflows/blank.yml diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml deleted file mode 100644 index c86a241..0000000 --- a/.github/workflows/blank.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Code Style - -on: [push, pull_request] -permissions: - contents: read -jobs: - fpm: - name: Fortran Code Formatting - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Install fprettify - run: | - sudo apt-get install -y python3-pip - pip3 install fprettify - - name: Format Fortran files - id: format - run: | - fprettify -i 4 --recursive . - - name: Fail if needs reformatting - run: | - if [[ $(git status --porcelain) ]]; then - echo "please reformat/fprettify these files:" - git diff - exit 1 - else - echo "All files are properly formatted." - fi From cf268b232166c3b9a0828584c485487615206d04 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 17:26:09 +0200 Subject: [PATCH 22/23] Implementing ctest-friendly tests for /searches modules --- .github/workflows/ci.yml | 2 +- tests/searches/linear_search.f90 | 122 +++++++++++++++++++++ tests/searches/recursive_linear_search.f90 | 122 +++++++++++++++++++++ tests/searches/ternary_search_array.f90 | 121 ++++++++++++++++++++ tests/searches/ternary_search_function.f90 | 85 ++++++++++++++ 5 files changed, 451 insertions(+), 1 deletion(-) create mode 100644 tests/searches/linear_search.f90 create mode 100644 tests/searches/recursive_linear_search.f90 create mode 100644 tests/searches/ternary_search_array.f90 create mode 100644 tests/searches/ternary_search_function.f90 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9c0fbb9..55b0e37 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,7 +41,7 @@ jobs: - name: Test working-directory: ${{env.build_path}} - run: ctest + run: ctest --output-on-failure - name: Run examples working-directory: ${{env.build_path}} diff --git a/tests/searches/linear_search.f90 b/tests/searches/linear_search.f90 new file mode 100644 index 0000000..8d23203 --- /dev/null +++ b/tests/searches/linear_search.f90 @@ -0,0 +1,122 @@ +!> Test program for the Linear Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides additional test cases to validate the linear_search_module. + + +program tests_linear_search + use linear_search_module + implicit none + integer, dimension(:), allocatable :: array + integer :: target, index, expected + + ! Run test cases + call test_found() + call test_not_found() + call test_first_element() + call test_last_element() + call test_multiple_occurrences() + call test_single_element_found() + call test_single_element_not_found() + call test_empty_array() + + print *, "All tests completed." + +contains + + ! Test case 1: Target is found in the array + subroutine test_found() + array = (/30, 10, 20, 40, 55, 61, 72, 86, 97, 101/) + target = 97 + expected = 9 + index = linear_search(array, target) + call assert_test(index, expected, "Test 1: Target found in the array") + end subroutine test_found + + ! Test case 2: Target is not found in the array + subroutine test_not_found() + array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/) + target = 66 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 2: Target not found in the array") + end subroutine test_not_found + + ! Test case 3: Target is the first element + subroutine test_first_element() + array = (/10, 20, 30, 40, 50/) + target = array(1) + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 3: Target is the first element") + end subroutine test_first_element + + ! Test case 4: Target is the last element + subroutine test_last_element() + array = (/10, 20, 30, 40, 50, 60, 70, 80/) + target = array(size(array)) + expected = size(array) + index = linear_search(array, target) + call assert_test(index, expected, "Test 4: Target is the last element") + end subroutine test_last_element + + ! Test case 5: Multiple occurrences of the target + subroutine test_multiple_occurrences() + array = (/1, 2, 3, 2, 4, 2, 5, 2, 4/) + target = 4 + expected = 5 + index = linear_search(array, target) + call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)") + end subroutine test_multiple_occurrences + + ! Test case 6: Single element found + subroutine test_single_element_found() + array = (/42/) + target = 42 + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 6: Single element found") + end subroutine test_single_element_found + + ! Test case 7: Single element not found + subroutine test_single_element_not_found() + array = (/42/) + target = 99 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 7: Single element not found") + end subroutine test_single_element_not_found + + ! Test case 8: Empty array + subroutine test_empty_array() + if (allocated(array)) deallocate (array) + allocate (array(0)) ! Empty array + target = 1 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 8: Search in an empty array") + end subroutine test_empty_array + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + integer, intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (actual == expected) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + +end program tests_linear_search diff --git a/tests/searches/recursive_linear_search.f90 b/tests/searches/recursive_linear_search.f90 new file mode 100644 index 0000000..d264e03 --- /dev/null +++ b/tests/searches/recursive_linear_search.f90 @@ -0,0 +1,122 @@ +!> Test program for the Recursive Linear Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides additional test cases to validate the recursive_linear_search_module. + + +program tests_recursive_linear_search + use linear_search_module + implicit none + integer, dimension(:), allocatable :: array + integer :: target, index, expected + + ! Run test cases + call test_found() + call test_not_found() + call test_first_element() + call test_last_element() + call test_multiple_occurrences() + call test_single_element_found() + call test_single_element_not_found() + call test_empty_array() + + print *, "All tests completed." + +contains + + ! Test case 1: Target is found in the array + subroutine test_found() + array = (/30, 10, 20, 40, 55, 61, 72, 86, 97, 101/) + target = 97 + expected = 9 + index = linear_search(array, target) + call assert_test(index, expected, "Test 1: Target found in the array") + end subroutine test_found + + ! Test case 2: Target is not found in the array + subroutine test_not_found() + array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/) + target = 66 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 2: Target not found in the array") + end subroutine test_not_found + + ! Test case 3: Target is the first element + subroutine test_first_element() + array = (/10, 20, 30, 40, 50/) + target = array(1) + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 3: Target is the first element") + end subroutine test_first_element + + ! Test case 4: Target is the last element + subroutine test_last_element() + array = (/10, 20, 30, 40, 50, 60, 70, 80/) + target = array(size(array)) + expected = size(array) + index = linear_search(array, target) + call assert_test(index, expected, "Test 4: Target is the last element") + end subroutine test_last_element + + ! Test case 5: Multiple occurrences of the target + subroutine test_multiple_occurrences() + array = (/1, 2, 3, 2, 4, 2, 5, 2, 4/) + target = 4 + expected = 5 + index = linear_search(array, target) + call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)") + end subroutine test_multiple_occurrences + + ! Test case 6: Single element found + subroutine test_single_element_found() + array = (/42/) + target = 42 + expected = 1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 6: Single element found") + end subroutine test_single_element_found + + ! Test case 7: Single element not found + subroutine test_single_element_not_found() + array = (/42/) + target = 99 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 7: Single element not found") + end subroutine test_single_element_not_found + + ! Test case 8: Empty array + subroutine test_empty_array() + if (allocated(array)) deallocate (array) + allocate (array(0)) ! Empty array + target = 1 + expected = -1 + index = linear_search(array, target) + call assert_test(index, expected, "Test 8: Search in an empty array") + end subroutine test_empty_array + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + integer, intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (actual == expected) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + +end program tests_recursive_linear_search diff --git a/tests/searches/ternary_search_array.f90 b/tests/searches/ternary_search_array.f90 new file mode 100644 index 0000000..b66ac1e --- /dev/null +++ b/tests/searches/ternary_search_array.f90 @@ -0,0 +1,121 @@ +!> Test program for the Array-Based Ternary Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides additional test cases to validate the array-based ternary_search module. + +program tests_ternary_search_array + use ternary_search + implicit none + integer, dimension(:), allocatable :: sorted_array + integer :: target, index, expected + + ! Run test cases + call test_found() + call test_not_found() + call test_first_element() + call test_last_element() + call test_multiple_occurrences() + call test_single_element_found() + call test_single_element_not_found() + call test_empty_array() + + print *, "All tests completed." + +contains + + ! Test case 1: Target found + subroutine test_found() + sorted_array = (/1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25/) + target = 21 + expected = 11 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 1: Target found in the array") + end subroutine test_found + + ! Test case 2: Target not found + subroutine test_not_found() + sorted_array = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12/) + target = 110 + expected = -1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, -1, "Test 2: Target not found in the array") + end subroutine test_not_found + + ! Test case 3: Target is the first element + subroutine test_first_element() + sorted_array = (/10, 20, 30, 40, 50, 60, 70, 80/) + target = sorted_array(1) + expected = 1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 3: Target is the first element") + end subroutine test_first_element + + ! Test case 4: Target is the last element + subroutine test_last_element() + sorted_array = (/100, 200, 300, 400, 500, 600, 700, 800, 900/) + target = sorted_array(size(sorted_array)) + expected = size(sorted_array) + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 4: Target is the last element") + end subroutine test_last_element + + ! Test case 5: Multiple occurrences of the target + subroutine test_multiple_occurrences() + sorted_array = (/1, 1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11, 12, 12/) + target = 12 + expected = 16 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 5: Target has multiple occurrences (first found)") + end subroutine test_multiple_occurrences + + ! Test case 6: Single element found + subroutine test_single_element_found() + sorted_array = (/59/) + target = 59 + expected = 1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 6: Single element found") + end subroutine test_single_element_found + + ! Test case 7: Single element not found + subroutine test_single_element_not_found() + sorted_array = (/42/) + target = 99 + expected = -1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 7: Single element not found") + end subroutine test_single_element_not_found + + ! Test case 8: Empty array + subroutine test_empty_array() + if (allocated(sorted_array)) deallocate (sorted_array) + allocate (sorted_array(0)) ! Empty array + target = 1 + expected = -1 + index = ternary_search_array(sorted_array, target, 1, size(sorted_array)) + call assert_test(index, expected, "Test 8: Search in an empty array") + end subroutine test_empty_array + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + integer, intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (actual == expected) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + +end program tests_ternary_search_array diff --git a/tests/searches/ternary_search_function.f90 b/tests/searches/ternary_search_function.f90 new file mode 100644 index 0000000..e137b46 --- /dev/null +++ b/tests/searches/ternary_search_function.f90 @@ -0,0 +1,85 @@ +!> Test program for the Function-Based Ternary Search algorithm +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #30 +!! https://github.com/TheAlgorithms/Fortran/pull/30 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program provides test cases to validate the function-based ternary search algorithms for known functions. + +program tests_ternary_search_function + use ternary_search + implicit none + real(8) :: tol, left, right, result, expected + + tol = 1.0d-6 + left = -100.0d0 + right = 100.0d0 + + ! Run test cases + call test_find_min_parabola() + call test_find_max_negative_parabola() + call test_find_min_custom_function() + + print *, "All tests completed." + +contains + + ! Test case 1: Find minimum of a parabola (f(x) = x^2) + subroutine test_find_min_parabola() + result = ternary_search_minimum(parabola, left, right, tol) + expected = 0.0d0 + call assert_test(abs(result), expected, "Test 1: Find minimum of f(x) = x^2") + end subroutine test_find_min_parabola + + ! Test case 2: Find maximum of a negative parabola (f(x) = -x^2) + subroutine test_find_max_negative_parabola() + result = ternary_search_maximum(negative_parabola, left, right, tol) + expected = 0.0d0 + call assert_test(abs(result), expected, "Test 2: Find maximum of f(x) = -x^2") + end subroutine test_find_max_negative_parabola + + ! Test case 3: Find minimum of a custom unimodal function + subroutine test_find_min_custom_function() + result = ternary_search_minimum(custom_unimodal_function, left, right, tol) + expected = 50.0d0 + call assert_test(result, 50.0d0, "Test 3: Find minimum of custom unimodal function") + end subroutine test_find_min_custom_function + + !> Subroutine to assert the test results + subroutine assert_test(actual, expected, test_name) + real(8), intent(in) :: actual, expected + character(len=*), intent(in) :: test_name + + if (abs(actual - expected) < tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 + end if + + end subroutine assert_test + + ! Parabola function: f(x) = x^2 + real(8) function parabola(x) + real(8), intent(in) :: x + parabola = x**2 + end function parabola + + ! Negative parabola function: f(x) = -x^2 + real(8) function negative_parabola(x) + real(8), intent(in) :: x + negative_parabola = -x**2 + end function negative_parabola + + ! Custom unimodal function: A function with a known minimum at x = 50 + real(8) function custom_unimodal_function(x) + real(8), intent(in) :: x + custom_unimodal_function = (x - 50.0d0)**2 + 100.0d0 + end function custom_unimodal_function + +end program tests_ternary_search_function From 76e7c83b237bf6cf320e77b2b1b5a3bf6232db8a Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Wed, 9 Oct 2024 17:39:55 +0200 Subject: [PATCH 23/23] code_style fix --- tests/searches/linear_search.f90 | 1 - tests/searches/recursive_linear_search.f90 | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/searches/linear_search.f90 b/tests/searches/linear_search.f90 index 8d23203..cb40616 100644 --- a/tests/searches/linear_search.f90 +++ b/tests/searches/linear_search.f90 @@ -9,7 +9,6 @@ !! !! This program provides additional test cases to validate the linear_search_module. - program tests_linear_search use linear_search_module implicit none diff --git a/tests/searches/recursive_linear_search.f90 b/tests/searches/recursive_linear_search.f90 index d264e03..38f4847 100644 --- a/tests/searches/recursive_linear_search.f90 +++ b/tests/searches/recursive_linear_search.f90 @@ -9,7 +9,6 @@ !! !! This program provides additional test cases to validate the recursive_linear_search_module. - program tests_recursive_linear_search use linear_search_module implicit none