diff --git a/src/tests/validity/array_get_negative_index_by_copy.ab b/src/tests/validity/array_get_negative_index_by_copy.ab new file mode 100644 index 00000000..7ace620b --- /dev/null +++ b/src/tests/validity/array_get_negative_index_by_copy.ab @@ -0,0 +1,30 @@ +// Output +// Value at -5: "" +// Value at -4: "zero" +// Value at -3: "one" +// Value at -2: "two" +// Value at -1: "three" + +// Replace this with builtin if/when we get around to writing one. +#[allow_absurd_cast] +fun bash_version(): Num { + let major = trust $ echo "\$\{BASH_VERSINFO[0]}" $ as Num + let minor = trust $ echo "\$\{BASH_VERSINFO[1]}" $ as Num + return (major * 100) + minor +} + +fun test_index(array) { + for index in -5..=-1 { + echo "Value at {index}: \"{array[index]}\"" + } +} + +main { + if bash_version() >= 402 { + let array = ["zero", "one", "two", "three"] + test_index(array) + } else { + // Negative indexing is not supported before Bash 4. + echo "Succeeded" + } +} diff --git a/src/tests/validity/array_get_negative_index_by_ref.ab b/src/tests/validity/array_get_negative_index_by_ref.ab new file mode 100644 index 00000000..993a82a3 --- /dev/null +++ b/src/tests/validity/array_get_negative_index_by_ref.ab @@ -0,0 +1,30 @@ +// Output +// Value at -5: "" +// Value at -4: "zero" +// Value at -3: "one" +// Value at -2: "two" +// Value at -1: "three" + +// Replace this with builtin if/when we get around to writing one. +#[allow_absurd_cast] +fun bash_version(): Num { + let major = trust $ echo "\$\{BASH_VERSINFO[0]}" $ as Num + let minor = trust $ echo "\$\{BASH_VERSINFO[1]}" $ as Num + return (major * 100) + minor +} + +fun test_index(ref byref) { + for index in -5..=-1 { + echo "Value at {index}: \"{byref[index]}\"" + } +} + +main { + if bash_version() >= 402 { + let array = ["zero", "one", "two", "three"] + test_index(array) + } else { + // Negative indexing is not supported before Bash 4. + echo "Succeeded" + } +} diff --git a/src/tests/validity/array_get_negative_index_local.ab b/src/tests/validity/array_get_negative_index_local.ab new file mode 100644 index 00000000..1b0d9f1d --- /dev/null +++ b/src/tests/validity/array_get_negative_index_local.ab @@ -0,0 +1,29 @@ +// Output +// Value at -5: "" +// Value at -4: "zero" +// Value at -3: "one" +// Value at -2: "two" +// Value at -1: "three" + +// Replace this with builtin if/when we get around to writing one. +#[allow_absurd_cast] +fun bash_version(): Num { + let major = trust $ echo "\$\{BASH_VERSINFO[0]}" $ as Num + let minor = trust $ echo "\$\{BASH_VERSINFO[1]}" $ as Num + return (major * 100) + minor +} + +main { + if bash_version() >= 402 { + // Do not use loop; we want to test compile time arithmetic. + let array = ["zero", "one", "two", "three"] + echo "Value at -5: \"{array[-5]}\"" + echo "Value at -4: \"{array[-4]}\"" + echo "Value at -3: \"{array[-3]}\"" + echo "Value at -2: \"{array[-2]}\"" + echo "Value at -1: \"{array[-1]}\"" + } else { + // Negative indexing is not supported before Bash 4. + echo "Succeeded" + } +} diff --git a/src/tests/validity/array_get_index_by_copy.ab b/src/tests/validity/array_get_positive_index_by_copy.ab similarity index 67% rename from src/tests/validity/array_get_index_by_copy.ab rename to src/tests/validity/array_get_positive_index_by_copy.ab index e1394566..0a246b42 100644 --- a/src/tests/validity/array_get_index_by_copy.ab +++ b/src/tests/validity/array_get_positive_index_by_copy.ab @@ -1,9 +1,4 @@ // Output -// Value at -5: "" -// Value at -4: "zero" -// Value at -3: "one" -// Value at -2: "two" -// Value at -1: "three" // Value at 0: "zero" // Value at 1: "one" // Value at 2: "two" @@ -11,7 +6,7 @@ // Value at 4: "" fun test_index(array) { - for index in -5..=4 { + for index in 0..=4 { echo "Value at {index}: \"{array[index]}\"" } } diff --git a/src/tests/validity/array_get_index_by_ref.ab b/src/tests/validity/array_get_positive_index_by_ref.ab similarity index 67% rename from src/tests/validity/array_get_index_by_ref.ab rename to src/tests/validity/array_get_positive_index_by_ref.ab index 6c2e7b4d..889af2ed 100644 --- a/src/tests/validity/array_get_index_by_ref.ab +++ b/src/tests/validity/array_get_positive_index_by_ref.ab @@ -1,9 +1,4 @@ // Output -// Value at -5: "" -// Value at -4: "zero" -// Value at -3: "one" -// Value at -2: "two" -// Value at -1: "three" // Value at 0: "zero" // Value at 1: "one" // Value at 2: "two" @@ -11,7 +6,7 @@ // Value at 4: "" fun test_index(ref byref) { - for index in -5..=4 { + for index in 0..=4 { echo "Value at {index}: \"{byref[index]}\"" } } diff --git a/src/tests/validity/array_get_index_local.ab b/src/tests/validity/array_get_positive_index_local.ab similarity index 57% rename from src/tests/validity/array_get_index_local.ab rename to src/tests/validity/array_get_positive_index_local.ab index 9ad9dd92..ed094170 100644 --- a/src/tests/validity/array_get_index_local.ab +++ b/src/tests/validity/array_get_positive_index_local.ab @@ -1,9 +1,4 @@ // Output -// Value at -5: "" -// Value at -4: "zero" -// Value at -3: "one" -// Value at -2: "two" -// Value at -1: "three" // Value at 0: "zero" // Value at 1: "one" // Value at 2: "two" @@ -13,11 +8,6 @@ main { // Do not use loop; we want to test compile time arithmetic. let array = ["zero", "one", "two", "three"] - echo "Value at -5: \"{array[-5]}\"" - echo "Value at -4: \"{array[-4]}\"" - echo "Value at -3: \"{array[-3]}\"" - echo "Value at -2: \"{array[-2]}\"" - echo "Value at -1: \"{array[-1]}\"" echo "Value at 0: \"{array[0]}\"" echo "Value at 1: \"{array[1]}\"" echo "Value at 2: \"{array[2]}\""