Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions exercises/practice/matrix/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{{ header }}
teardown() {
rm input.txt
}
{% for idx, case in cases %}
@test "{{ case["description"] }}" {
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
printf "%s\n" ${{ case["input"]["string"] | repr }} > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::{{ case["property"] }}(mtx, {{ case["input"]["index"] }})
}
'
assert_success
assert_output "{{ case["expected"] | join(" ") }}"
}
{% endfor %}
@test "can extract row from non-square matrix" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::row(mtx, 3)
}
'

assert_success
assert_output "7 8 9"
}

@test "can extract column from non-square matrix" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::column(mtx, 3)
}
'

assert_success
assert_output "3 6 9 6"
}

85 changes: 33 additions & 52 deletions exercises/practice/matrix/test-matrix.bats
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#!/usr/bin/env bats
load bats-extra

# generated on 2026-06-30T23:03:29+00:00
teardown() {
rm input.txt
}

# row tests

@test "extract row from one number matrix" {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1" > input.txt
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
printf "%s\n" $'1' > input.txt

run gawk '
@include "matrix"
Expand All @@ -19,15 +17,13 @@ teardown() {
print matrix::row(mtx, 1)
}
'

assert_success
assert_output "1"
}

@test "can extract row" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2" "3 4" > input.txt
printf "%s\n" $'1 2\n3 4' > input.txt

run gawk '
@include "matrix"
Expand All @@ -36,15 +32,13 @@ teardown() {
print matrix::row(mtx, 2)
}
'

assert_success
assert_output "3 4"
}

@test "extract row where numbers have different widths" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2" "10 20" > input.txt
printf "%s\n" $'1 2\n10 20' > input.txt

run gawk '
@include "matrix"
Expand All @@ -53,128 +47,115 @@ teardown() {
print matrix::row(mtx, 2)
}
'

assert_success
assert_output "10 20"
}

@test "can extract row from non-square matrix" {
@test "can extract row from non-square matrix with no corresponding column" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2 3" "4 5 6" "7 8 9" "8 7 6" > input.txt
printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::row(mtx, 3)
print matrix::row(mtx, 4)
}
'

assert_success
assert_output "7 8 9"
assert_output "8 7 6"
}

@test "can extract row from non-square matrix with no corresponding column" {
@test "extract column from one number matrix" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2 3" "4 5 6" "7 8 9" "8 7 6" > input.txt
printf "%s\n" $'1' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::row(mtx, 4)
print matrix::column(mtx, 1)
}
'

assert_success
assert_output "8 7 6"
assert_output "1"
}

# column tests

@test "extract column from one number matrix" {
@test "can extract column" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1" > input.txt
printf "%s\n" $'1 2 3\n4 5 6\n7 8 9' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::column(mtx, 1)
print matrix::column(mtx, 3)
}
'

assert_success
assert_output "1"
assert_output "3 6 9"
}

@test "can extract column" {
@test "can extract column from non-square matrix with no corresponding row" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2 3" "4 5 6" "7 8 9" > input.txt
printf "%s\n" $'1 2 3 4\n5 6 7 8\n9 8 7 6' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::column(mtx, 3)
print matrix::column(mtx, 4)
}
'

assert_success
assert_output "3 6 9"
assert_output "4 8 6"
}

@test "can extract column from non-square matrix" {
@test "extract column where numbers have different widths" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2 3" "4 5 6" "7 8 9" "8 7 6" > input.txt
printf "%s\n" $'89 1903 3\n18 3 1\n9 4 800' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::column(mtx, 3)
print matrix::column(mtx, 2)
}
'

assert_success
assert_output "3 6 9 6"
assert_output "1903 3 4"
}

@test "can extract column from non-square matrix with no corresponding row" {
@test "can extract row from non-square matrix" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "1 2 3 4" "5 6 7 8" "9 8 7 6" > input.txt
printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::column(mtx, 4)
print matrix::row(mtx, 3)
}
'

assert_success
assert_output "4 8 6"
assert_output "7 8 9"
}

@test "extract column where numbers have different widths" {
@test "can extract column from non-square matrix" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

printf "%s\n" "89 1903 3" "18 3 1" "9 4 800" > input.txt
printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt

run gawk '
@include "matrix"
BEGIN {
matrix::read("input.txt", mtx)
print matrix::column(mtx, 2)
print matrix::column(mtx, 3)
}
'

assert_success
assert_output "1903 3 4"
assert_output "3 6 9 6"
}