Skip to content
Open
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
20 changes: 20 additions & 0 deletions exercises/practice/satellite/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ header }}
{% for idx, case in cases %}
@test "{{ case["description"] }}" {
{% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash {{ solution }} "{{ case["input"]["preorder"] | join(" ") }}" "{{ case["input"]["inorder"] | join(" ") }}"
{%- if case["expect_error"] %}
assert_failure
shopt -s nocasematch
assert_output "{{ case["expect_error_msg"] }}"
{%- else %}
assert_success
expectedJson=$(cat << END_JSON
{{ case["expected"] | tojson(separators=None, indent=4) | indent(4, first=True) }}
END_JSON
)
# compare without spaces
[[ "${output//[[:space:]]/}" == "${expectedJson//[[:space:]]/}" ]]
{%- endif %}
}
{% endfor %}
89 changes: 70 additions & 19 deletions exercises/practice/satellite/satellite.bats
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
#!/usr/bin/env bats
load bats-extra

# generated on 2026-06-28T23:19:01+00:00
# local version: 2.0.0.0

@test "Empty tree" {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
# [[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash satellite.sh "" ""
assert_success
[[ $output = "{}" ]]
expectedJson=$(cat << END_JSON
{}
END_JSON
)
# compare without spaces
[[ "${output//[[:space:]]/}" == "${expectedJson//[[:space:]]/}" ]]
}

@test "Tree with one item" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash satellite.sh "a" "a"
assert_success
expected='{"v": "a", "l": {}, "r": {}}'
assert_output "$expected"
expectedJson=$(cat << END_JSON
{
"v": "a",
"l": {},
"r": {}
}
END_JSON
)
# compare without spaces
[[ "${output//[[:space:]]/}" == "${expectedJson//[[:space:]]/}" ]]
}

@test "Tree with many items" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash satellite.sh "a i x f r" "i a f x r"
assert_success

expectedJson=$(cat << END_JSON
{"v": "a",
"l": {"v": "i", "l": {}, "r": {}},
"r": {"v": "x",
"l": {"v": "f", "l": {}, "r": {}},
"r": {"v": "r", "l": {}, "r": {}}
{
"v": "a",
"l": {
"v": "i",
"l": {},
"r": {}
},
"r": {
"v": "x",
"l": {
"v": "f",
"l": {},
"r": {}
},
"r": {
"v": "r",
"l": {},
"r": {}
}
}
}
END_JSON
Expand All @@ -47,18 +74,18 @@ END_JSON

@test "Reject inconsistent traversals of same length" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash satellite.sh "a b c" "x y z"
run bash satellite.sh "x y z" "a b c"
assert_failure
shopt -s nocasematch
assert_output "traversals must have the same elements"
}

@test "Reject traversals with repeated elements" {
@test "Reject traversals with repeated items" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash satellite.sh "a b a" "b a a"
assert_failure
shopt -s nocasematch
assert_output "traversals must contain unique elements"
assert_output "traversals must contain unique items"
}

@test "A degenerate binary tree" {
Expand All @@ -72,7 +99,11 @@ END_JSON
"v": "b",
"l": {
"v": "c",
"l": { "v": "d", "l": {}, "r": {} },
"l": {
"v": "d",
"l": {},
"r": {}
},
"r": {}
},
"r": {}
Expand All @@ -99,7 +130,11 @@ END_JSON
"r": {
"v": "c",
"l": {},
"r": { "v": "d", "l": {}, "r": {} }
"r": {
"v": "d",
"l": {},
"r": {}
}
}
}
}
Expand All @@ -120,17 +155,33 @@ END_JSON
"v": "b",
"l": {
"v": "d",
"l": { "v": "g", "l": {}, "r": {} },
"r": { "v": "h", "l": {}, "r": {} }
"l": {
"v": "g",
"l": {},
"r": {}
},
"r": {
"v": "h",
"l": {},
"r": {}
}
},
"r": {}
},
"r": {
"v": "c",
"l": { "v": "e", "l": {}, "r": {} },
"l": {
"v": "e",
"l": {},
"r": {}
},
"r": {
"v": "f",
"l": { "v": "i", "l": {}, "r": {} },
"l": {
"v": "i",
"l": {},
"r": {}
},
"r": {}
}
}
Expand Down
Loading