Skip to content

Commit

Permalink
Test negative indices on Bash 4.2 and above.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdwalters committed Dec 16, 2024
1 parent a3d2eac commit b71bda7
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 22 deletions.
30 changes: 30 additions & 0 deletions src/tests/validity/array_get_negative_index_by_copy.ab
Original file line number Diff line number Diff line change
@@ -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"
}
}
30 changes: 30 additions & 0 deletions src/tests/validity/array_get_negative_index_by_ref.ab
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 29 additions & 0 deletions src/tests/validity/array_get_negative_index_local.ab
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// 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"
// Value at 3: "three"
// Value at 4: ""

fun test_index(array) {
for index in -5..=4 {
for index in 0..=4 {
echo "Value at {index}: \"{array[index]}\""
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// 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"
// Value at 3: "three"
// Value at 4: ""

fun test_index(ref byref) {
for index in -5..=4 {
for index in 0..=4 {
echo "Value at {index}: \"{byref[index]}\""
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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]}\""
Expand Down

0 comments on commit b71bda7

Please sign in to comment.