diff options
Diffstat (limited to 'js/scripting-lang/tests/turing-completeness/07_complex_algorithms.txt')
-rw-r--r-- | js/scripting-lang/tests/turing-completeness/07_complex_algorithms.txt | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/js/scripting-lang/tests/turing-completeness/07_complex_algorithms.txt b/js/scripting-lang/tests/turing-completeness/07_complex_algorithms.txt new file mode 100644 index 0000000..89b07df --- /dev/null +++ b/js/scripting-lang/tests/turing-completeness/07_complex_algorithms.txt @@ -0,0 +1,104 @@ +/* Turing Completeness: Complex Algorithms */ +/* Demonstrates: Non-trivial algorithms, data manipulation, computational complexity */ + +..out "=== Complex Algorithms Test ==="; + +/* Test 1: Euclidean Algorithm (GCD) */ +gcd : a b -> when b is 0 then a _ then gcd b (a % b); + +gcd_test1 : gcd 48 18; +gcd_test2 : gcd 101 13; + +..assert gcd_test1 = 6; +..assert gcd_test2 = 1; +..out "1. Euclidean algorithm (GCD): PASS"; + +/* Test 2: Prime number checking */ +is_divisible : n d -> n % d = 0; + +/* Check if number is prime (simplified version) */ +prime_helper : n d -> when d * d > n is + true then true + false then when is_divisible n d is + true then false + false then prime_helper n (d + 1); + +is_prime : n -> when n < 2 is + true then false + false then prime_helper n 2; + +prime_test1 : is_prime 17; +prime_test2 : is_prime 15; +prime_test3 : is_prime 2; + +..assert prime_test1 = true; +..assert prime_test2 = false; +..assert prime_test3 = true; +..out "2. Prime number checking: PASS"; + +/* Test 3: Binary search simulation */ +/* Since we can't mutate arrays, we simulate with range checking */ +binary_search_step : target low high -> when low > high is + true then -1 + false then { + mid: (low + high) / 2, + mid_val: mid, /* In real implementation, this would be array[mid] */ + result: when target = mid is + true then mid + false then when target < mid is + true then binary_search_step target low (mid - 1) + false then binary_search_step target (mid + 1) high + }.result; + +/* Test binary search logic */ +search_test : binary_search_step 7 0 10; + +..assert search_test = 7; +..out "3. Binary search logic: PASS"; + +/* Test 4: Sorting algorithm (insertion sort concept) */ +/* Function to insert element in sorted position */ +insert_sorted : x sorted_list -> when sorted_list is + [] then [x] + h :: t then when x <= h is + true then x :: sorted_list + false then h :: (insert_sorted x t); + +/* Insertion sort */ +insertion_sort : list -> when list is + [] then [] + h :: t then insert_sorted h (insertion_sort t); + +sort_test : insertion_sort [3, 1, 4, 1, 5, 9, 2]; + +..assert sort_test = [1, 1, 2, 3, 4, 5, 9]; +..out "4. Insertion sort: PASS"; + +/* Test 5: Tree traversal */ +/* Binary tree: {value, left, right} */ +tree : { + value: 10, + left: { + value: 5, + left: {value: 3, left: null, right: null}, + right: {value: 7, left: null, right: null} + }, + right: { + value: 15, + left: {value: 12, left: null, right: null}, + right: {value: 18, left: null, right: null} + } +}; + +/* In-order traversal */ +inorder : tree -> when tree is + null then [] + _ then (inorder tree.left) concat [tree.value] concat (inorder tree.right); + +traversal_result : inorder tree; + +..assert traversal_result = [3, 5, 7, 10, 12, 15, 18]; +..out "5. Tree traversal: PASS"; + +..out "✅ All complex algorithm tests passed"; +..out "Demonstrates: Advanced algorithms, recursive data structures, computational efficiency"; \ No newline at end of file |