Skip to content

Commit 4ab1419

Browse files
authored
Templates for: all-your-base, atbash-cipher and binary-search (#491)
* add templates for all-your-base, atbash-cipher, binary-search * correct variable name in atbash-cipher * format all-your-base config.json [no important files changed]
1 parent 2c72d03 commit 4ab1419

9 files changed

Lines changed: 177 additions & 66 deletions

File tree

exercises/practice/acronym/.meta/template.j2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ test_that("{{ case["description"] }}", {
1010
acronym(input), "{{ case["expected"] }}"
1111
)
1212
})
13-
14-
{% endfor %}
13+
{% endfor %}

exercises/practice/all-your-base/.meta/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"authors": ["colinleach"],
2+
"authors": [
3+
"colinleach"
4+
],
35
"files": {
46
"solution": [
57
"all-your-base.R"

exercises/practice/all-your-base/.meta/example.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from_digits <- function(digits, base) {
32
sum(digits * base^((length(digits) - 1):0))
43
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.canonical_ref() }}
3+
4+
{{ macros.header() }}
5+
6+
{% for case in cases -%}
7+
test_that("{{ case["description"] }}", {
8+
input_base <- {{ case["input"]["inputBase"] }}
9+
digits <- c{{ case["input"]["digits"] | list_to_tuple }}
10+
output_base <- {{ case["input"]["outputBase"] }}
11+
{% if case is error_case -%}
12+
expect_error(
13+
rebase(input_base, digits, output_base),
14+
"{{ case["expected"]["error"] }}"
15+
)
16+
{% else -%}
17+
expect_equal(
18+
rebase(input_base, digits, output_base),
19+
c{{ case["expected"] | list_to_tuple }}
20+
)
21+
{% endif -%}
22+
})
23+
{% endfor %}

exercises/practice/all-your-base/test_all-your-base.R

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,133 @@
1+
# These tests are auto-generated with test data from:
2+
# https://github.com/exercism/problem-specifications/tree/main/exercises/all-your-base/canonical-data.json
3+
# File last updated on 2026-03-16
4+
15
source("./all-your-base.R")
26
library(testthat)
37

4-
58
test_that("single bit one to decimal", {
6-
digits <- 1
79
input_base <- 2
10+
digits <- c(1)
811
output_base <- 10
9-
expect_equal(rebase(input_base, digits, output_base), 1)
12+
expect_equal(
13+
rebase(input_base, digits, output_base),
14+
c(1)
15+
)
1016
})
1117

1218
test_that("binary to single decimal", {
13-
digits <- c(1, 0, 1)
1419
input_base <- 2
20+
digits <- c(1, 0, 1)
1521
output_base <- 10
16-
expect_equal(rebase(input_base, digits, output_base), 5)
22+
expect_equal(
23+
rebase(input_base, digits, output_base),
24+
c(5)
25+
)
1726
})
1827

1928
test_that("single decimal to binary", {
20-
digits <- c(5)
2129
input_base <- 10
30+
digits <- c(5)
2231
output_base <- 2
23-
expect_equal(rebase(input_base, digits, output_base), c(1, 0, 1))
32+
expect_equal(
33+
rebase(input_base, digits, output_base),
34+
c(1, 0, 1)
35+
)
2436
})
2537

2638
test_that("binary to multiple decimal", {
27-
digits <- c(1, 0, 1, 0, 1, 0)
2839
input_base <- 2
40+
digits <- c(1, 0, 1, 0, 1, 0)
2941
output_base <- 10
30-
expect_equal(rebase(input_base, digits, output_base), c(4, 2))
42+
expect_equal(
43+
rebase(input_base, digits, output_base),
44+
c(4, 2)
45+
)
3146
})
3247

3348
test_that("decimal to binary", {
34-
digits <- c(4, 2)
3549
input_base <- 10
50+
digits <- c(4, 2)
3651
output_base <- 2
37-
expect_equal(rebase(input_base, digits, output_base), c(1, 0, 1, 0, 1, 0))
52+
expect_equal(
53+
rebase(input_base, digits, output_base),
54+
c(1, 0, 1, 0, 1, 0)
55+
)
3856
})
3957

4058
test_that("trinary to hexadecimal", {
41-
digits <- c(1, 1, 2, 0)
4259
input_base <- 3
60+
digits <- c(1, 1, 2, 0)
4361
output_base <- 16
44-
expect_equal(rebase(input_base, digits, output_base), c(2, 10))
62+
expect_equal(
63+
rebase(input_base, digits, output_base),
64+
c(2, 10)
65+
)
4566
})
4667

4768
test_that("hexadecimal to trinary", {
48-
digits <- c(2, 10)
4969
input_base <- 16
70+
digits <- c(2, 10)
5071
output_base <- 3
51-
expect_equal(rebase(input_base, digits, output_base), c(1, 1, 2, 0))
72+
expect_equal(
73+
rebase(input_base, digits, output_base),
74+
c(1, 1, 2, 0)
75+
)
5276
})
5377

5478
test_that("15-bit integer", {
55-
digits <- c(3, 46, 60)
5679
input_base <- 97
80+
digits <- c(3, 46, 60)
5781
output_base <- 73
58-
expect_equal(rebase(input_base, digits, output_base), c(6, 10, 45))
82+
expect_equal(
83+
rebase(input_base, digits, output_base),
84+
c(6, 10, 45)
85+
)
5986
})
6087

6188
test_that("empty list", {
62-
digits <- c()
6389
input_base <- 2
90+
digits <- c()
6491
output_base <- 10
65-
expect_equal(rebase(input_base, digits, output_base), 0)
92+
expect_equal(
93+
rebase(input_base, digits, output_base),
94+
c(0)
95+
)
6696
})
6797

6898
test_that("single zero", {
69-
digits <- c(0)
7099
input_base <- 10
100+
digits <- c(0)
71101
output_base <- 2
72-
expect_equal(rebase(input_base, digits, output_base), 0)
102+
expect_equal(
103+
rebase(input_base, digits, output_base),
104+
c(0)
105+
)
73106
})
74107

75108
test_that("multiple zeros", {
76-
digits <- c(0, 0, 0)
77109
input_base <- 10
110+
digits <- c(0, 0, 0)
78111
output_base <- 2
79-
expect_equal(rebase(input_base, digits, output_base), 0)
112+
expect_equal(
113+
rebase(input_base, digits, output_base),
114+
c(0)
115+
)
80116
})
81117

82118
test_that("leading zeros", {
83-
digits <- c(0, 6, 0)
84119
input_base <- 7
120+
digits <- c(0, 6, 0)
85121
output_base <- 10
86-
expect_equal(rebase(input_base, digits, output_base), c(4, 2))
122+
expect_equal(
123+
rebase(input_base, digits, output_base),
124+
c(4, 2)
125+
)
87126
})
88127

89128
test_that("input base is one", {
90-
digits <- c(0)
91129
input_base <- 1
130+
digits <- c(0)
92131
output_base <- 10
93132
expect_error(
94133
rebase(input_base, digits, output_base),
@@ -97,8 +136,8 @@ test_that("input base is one", {
97136
})
98137

99138
test_that("input base is zero", {
100-
digits <- c()
101139
input_base <- 0
140+
digits <- c()
102141
output_base <- 10
103142
expect_error(
104143
rebase(input_base, digits, output_base),
@@ -107,8 +146,8 @@ test_that("input base is zero", {
107146
})
108147

109148
test_that("input base is negative", {
110-
digits <- c(1)
111149
input_base <- -2
150+
digits <- c(1)
112151
output_base <- 10
113152
expect_error(
114153
rebase(input_base, digits, output_base),
@@ -117,8 +156,8 @@ test_that("input base is negative", {
117156
})
118157

119158
test_that("negative digit", {
120-
digits <- c(1, -1, 1, 0, 1, 0)
121159
input_base <- 2
160+
digits <- c(1, -1, 1, 0, 1, 0)
122161
output_base <- 10
123162
expect_error(
124163
rebase(input_base, digits, output_base),
@@ -127,8 +166,8 @@ test_that("negative digit", {
127166
})
128167

129168
test_that("invalid positive digit", {
130-
digits <- c(1, 2, 1, 0, 1, 0)
131169
input_base <- 2
170+
digits <- c(1, 2, 1, 0, 1, 0)
132171
output_base <- 10
133172
expect_error(
134173
rebase(input_base, digits, output_base),
@@ -137,8 +176,8 @@ test_that("invalid positive digit", {
137176
})
138177

139178
test_that("output base is one", {
140-
digits <- c(1, 0, 1, 0, 1, 0)
141179
input_base <- 2
180+
digits <- c(1, 0, 1, 0, 1, 0)
142181
output_base <- 1
143182
expect_error(
144183
rebase(input_base, digits, output_base),
@@ -147,8 +186,8 @@ test_that("output base is one", {
147186
})
148187

149188
test_that("output base is zero", {
150-
digits <- c(7)
151189
input_base <- 10
190+
digits <- c(7)
152191
output_base <- 0
153192
expect_error(
154193
rebase(input_base, digits, output_base),
@@ -157,8 +196,8 @@ test_that("output base is zero", {
157196
})
158197

159198
test_that("output base is negative", {
160-
digits <- c(1)
161199
input_base <- 2
200+
digits <- c(1)
162201
output_base <- -7
163202
expect_error(
164203
rebase(input_base, digits, output_base),
@@ -167,13 +206,11 @@ test_that("output base is negative", {
167206
})
168207

169208
test_that("both bases are negative", {
170-
digits <- c(1)
171209
input_base <- -2
210+
digits <- c(1)
172211
output_base <- -7
173212
expect_error(
174213
rebase(input_base, digits, output_base),
175214
"input base must be >= 2"
176215
)
177216
})
178-
179-
message("All tests passed for exercise: all-your-base")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.canonical_ref() }}
3+
4+
{{ macros.header() }}
5+
6+
{% for case in cases %}
7+
# {{ case["description"] }}
8+
{% for subcase in case["cases"] %}
9+
test_that("{{ subcase["description"] }}", {
10+
{% if subcase["property"] == "encode" %}
11+
plaintext <- "{{ subcase["input"]["phrase"] }}"
12+
expected <- "{{ subcase["expected"] }}"
13+
expect_equal(encode(plaintext), expected)
14+
{% else %}
15+
ciphertext <- "{{ subcase["input"]["phrase"] }}"
16+
expected <- "{{ subcase["expected"] }}"
17+
expect_equal(decode(ciphertext), expected)
18+
{% endif %}
19+
})
20+
{% endfor %}
21+
{% endfor %}

0 commit comments

Comments
 (0)