aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--src/cube.h11
-rw-r--r--src/cube_array.c118
-rw-r--r--test/010_io_H48/00_garbage.in (renamed from test/01_io/00_garbage.in)0
-rw-r--r--test/010_io_H48/00_garbage.out (renamed from test/01_io/00_garbage.out)0
-rw-r--r--test/010_io_H48/01_solved_oneline.in (renamed from test/01_io/01_solved_oneline.in)0
-rw-r--r--test/010_io_H48/01_solved_oneline.out (renamed from test/01_io/01_solved_oneline.out)0
-rw-r--r--test/010_io_H48/02_solved_oneline_whitespace.in (renamed from test/01_io/02_solved_oneline_whitespace.in)0
-rw-r--r--test/010_io_H48/02_solved_oneline_whitespace.out (renamed from test/01_io/02_solved_oneline_whitespace.out)0
-rw-r--r--test/010_io_H48/03_solved_multiline.in (renamed from test/01_io/03_solved_multiline.in)0
-rw-r--r--test/010_io_H48/03_solved_multiline.out (renamed from test/01_io/03_solved_multiline.out)0
-rw-r--r--test/010_io_H48/04_unsolvable_ep.in (renamed from test/01_io/04_unsolvable_ep.in)0
-rw-r--r--test/010_io_H48/04_unsolvable_ep.out (renamed from test/01_io/04_unsolvable_ep.out)0
-rw-r--r--test/010_io_H48/05_unsolvable_eo.in (renamed from test/01_io/05_unsolvable_eo.in)0
-rw-r--r--test/010_io_H48/05_unsolvable_eo.out (renamed from test/01_io/05_unsolvable_eo.out)0
-rw-r--r--test/010_io_H48/06_unsolvable_cp.in (renamed from test/01_io/06_unsolvable_cp.in)0
-rw-r--r--test/010_io_H48/06_unsolvable_cp.out (renamed from test/01_io/06_unsolvable_cp.out)0
-rw-r--r--test/010_io_H48/07_unsolvable_co.in (renamed from test/01_io/07_unsolvable_co.in)0
-rw-r--r--test/010_io_H48/07_unsolvable_co.out (renamed from test/01_io/07_unsolvable_co.out)0
-rw-r--r--test/010_io_H48/08_unsolved.in (renamed from test/01_io/08_unsolved.in)0
-rw-r--r--test/010_io_H48/08_unsolved.out (renamed from test/01_io/08_unsolved.out)0
-rw-r--r--test/010_io_H48/io_H48_tests.c (renamed from test/01_io/io_tests.c)4
-rw-r--r--test/011_io_SRC/01_solved.in1
-rw-r--r--test/011_io_SRC/01_solved.out4
-rw-r--r--test/011_io_SRC/io_SRC_tests.c28
-rw-r--r--test/02_move/move_tests.c4
-rw-r--r--test/03_inverse/inverse_tests.c4
-rw-r--r--test/04_compose/compose_tests.c6
-rwxr-xr-xtest/test.sh2
-rw-r--r--utils/FORMAT.txt15
30 files changed, 161 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore
index e0538cd..5f06d9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ test/*/runtest
2test/run 2test/run
3test/last.* 3test/last.*
4test/*.o 4test/*.o
5*.o
diff --git a/src/cube.h b/src/cube.h
index 3319b1e..129062d 100644
--- a/src/cube.h
+++ b/src/cube.h
@@ -7,12 +7,11 @@ typedef uint8_t trans_t;
7 7
8extern cube_t solvedcube; 8extern cube_t solvedcube;
9 9
10/* For the textual representation of the cube, see utils/FORMAT.txt */ 10/* Not all formats are supported for both read and write. */
11cube_t readcube(char *); 11/* See utils/FORMAT.txt for details. */
12void writecube(cube_t, char *); 12typedef enum {H48, SRC} format_t;
13 13cube_t readcube(format_t, char *);
14/* Writes a cube in C source code format */ 14void writecube(format_t, cube_t, char *);
15void writecubesrc(cube_t, char *);
16 15
17bool issolvable(cube_t); 16bool issolvable(cube_t);
18bool equal(cube_t, cube_t); 17bool equal(cube_t, cube_t);
diff --git a/src/cube_array.c b/src/cube_array.c
index a9b9709..1d2da86 100644
--- a/src/cube_array.c
+++ b/src/cube_array.c
@@ -156,6 +156,10 @@ static uint8_t readeo(char *);
156static uint8_t readep(char *); 156static uint8_t readep(char *);
157static uint8_t readmove(char); 157static uint8_t readmove(char);
158static uint8_t readmodifier(char); 158static uint8_t readmodifier(char);
159static cube_t readcube_H48(char *);
160static void writecube_H48(cube_t, char *);
161static int writepiece_SRC(uint8_t, char *);
162static void writecube_SRC(cube_t, char *);
159static int permsign(uint8_t *, int); 163static int permsign(uint8_t *, int);
160 164
161static uint8_t 165static uint8_t
@@ -219,8 +223,8 @@ readep(char *str)
219 return _error; 223 return _error;
220} 224}
221 225
222cube_t 226static cube_t
223readcube(char *buf) 227readcube_H48(char *buf)
224{ 228{
225 int i; 229 int i;
226 uint8_t piece, orient; 230 uint8_t piece, orient;
@@ -231,10 +235,10 @@ readcube(char *buf)
231 while (*b == ' ' || *b == '\t' || *b == '\n') 235 while (*b == ' ' || *b == '\t' || *b == '\n')
232 b++; 236 b++;
233 if ((piece = readep(b)) == _error) 237 if ((piece = readep(b)) == _error)
234 goto readcube_error; 238 return errorcube;
235 b += 2; 239 b += 2;
236 if ((orient = readeo(b)) == _error) 240 if ((orient = readeo(b)) == _error)
237 goto readcube_error; 241 return errorcube;
238 b++; 242 b++;
239 ret.e[i] = piece | orient; 243 ret.e[i] = piece | orient;
240 } 244 }
@@ -242,36 +246,46 @@ readcube(char *buf)
242 while (*b == ' ' || *b == '\t' || *b == '\n') 246 while (*b == ' ' || *b == '\t' || *b == '\n')
243 b++; 247 b++;
244 if ((piece = readcp(b)) == _error) 248 if ((piece = readcp(b)) == _error)
245 goto readcube_error; 249 return errorcube;
246 b += 3; 250 b += 3;
247 if ((orient = readco(b)) == _error) 251 if ((orient = readco(b)) == _error)
248 goto readcube_error; 252 return errorcube;
249 b++; 253 b++;
250 ret.c[i] = piece | orient; 254 ret.c[i] = piece | orient;
251 } 255 }
252 256
253 return ret; 257 return ret;
258}
259
260cube_t
261readcube(format_t format, char *buf)
262{
263 cube_t ret;
264
265 switch (format) {
266 case H48:
267 ret = readcube_H48(buf);
268 break;
269 default:
270 #ifdef DEBUG
271 fprintf(stderr, "Cannot read cube in the given format\n");
272 #endif
273 ret = errorcube;
274 }
254 275
255readcube_error:
256#ifdef DEBUG 276#ifdef DEBUG
257 fprintf(stderr, "readcube error\n"); 277 if (iserror(ret))
278 fprintf(stderr, "readcube error\n");
258#endif 279#endif
259 return errorcube; 280 return ret;
260} 281}
261 282
262void 283static void
263writecube(cube_t cube, char *buf) 284writecube_H48(cube_t cube, char *buf)
264{ 285{
265 char *errormsg;
266 uint8_t piece, orient; 286 uint8_t piece, orient;
267 size_t len;
268 int i; 287 int i;
269 288
270 if (!isconsistent(cube)) {
271 errormsg = "ERROR: cannot write inconsistent cube";
272 goto writecube_error;
273 }
274
275 for (i = 0; i < 12; i++) { 289 for (i = 0; i < 12; i++) {
276 piece = cube.e[i] & _pbits; 290 piece = cube.e[i] & _pbits;
277 orient = (cube.e[i] & _eobit) >> _eoshift; 291 orient = (cube.e[i] & _eobit) >> _eoshift;
@@ -291,6 +305,73 @@ writecube(cube_t cube, char *buf)
291 } 305 }
292 306
293 buf[48+39] = '\0'; 307 buf[48+39] = '\0';
308}
309
310static int
311writepiece_SRC(uint8_t piece, char *buf)
312{
313 char digits[3];
314 int i, len = 0;
315
316 while (piece != 0) {
317 digits[len++] = (piece % 10) + '0';
318 piece /= 10;
319 }
320
321 if (len == 0)
322 digits[len++] = '0';
323
324 for (i = 0; i < len; i++)
325 buf[i] = digits[len-i-1];
326
327 buf[len] = ',';
328 buf[len+1] = ' ';
329
330 return len+2;
331}
332
333static void
334writecube_SRC(cube_t cube, char *buf)
335{
336 int i, ptr;
337
338 memcpy(buf, "{\n\t.c = {", 9);
339 ptr = 9;
340
341 for (i = 0; i < 8; i++)
342 ptr += writepiece_SRC(cube.c[i], buf + ptr);
343
344 memcpy(buf+ptr-2, "},\n\t.e = {", 10);
345 ptr += 8;
346
347 for (i = 0; i < 12; i++)
348 ptr += writepiece_SRC(cube.e[i], buf + ptr);
349
350 memcpy(buf+ptr-2, "}\n}\0", 4);
351}
352
353void
354writecube(format_t format, cube_t cube, char *buf)
355{
356 char *errormsg;
357 size_t len;
358
359 if (!isconsistent(cube)) {
360 errormsg = "ERROR: cannot write inconsistent cube";
361 goto writecube_error;
362 }
363
364 switch (format) {
365 case H48:
366 writecube_H48(cube, buf);
367 break;
368 case SRC:
369 writecube_SRC(cube, buf);
370 break;
371 default:
372 errormsg = "ERROR: cannot write cube in the given format";
373 goto writecube_error;
374 }
294 375
295 return; 376 return;
296 377
@@ -304,7 +385,6 @@ writecube_error:
304 buf[len+1] = '\0'; 385 buf[len+1] = '\0';
305} 386}
306 387
307
308static uint8_t 388static uint8_t
309readmove(char c) 389readmove(char c)
310{ 390{
diff --git a/test/01_io/00_garbage.in b/test/010_io_H48/00_garbage.in
index 9ac203a..9ac203a 100644
--- a/test/01_io/00_garbage.in
+++ b/test/010_io_H48/00_garbage.in
diff --git a/test/01_io/00_garbage.out b/test/010_io_H48/00_garbage.out
index 5b9114e..5b9114e 100644
--- a/test/01_io/00_garbage.out
+++ b/test/010_io_H48/00_garbage.out
diff --git a/test/01_io/01_solved_oneline.in b/test/010_io_H48/01_solved_oneline.in
index dff224d..dff224d 100644
--- a/test/01_io/01_solved_oneline.in
+++ b/test/010_io_H48/01_solved_oneline.in
diff --git a/test/01_io/01_solved_oneline.out b/test/010_io_H48/01_solved_oneline.out
index dff224d..dff224d 100644
--- a/test/01_io/01_solved_oneline.out
+++ b/test/010_io_H48/01_solved_oneline.out
diff --git a/test/01_io/02_solved_oneline_whitespace.in b/test/010_io_H48/02_solved_oneline_whitespace.in
index 5d10368..5d10368 100644
--- a/test/01_io/02_solved_oneline_whitespace.in
+++ b/test/010_io_H48/02_solved_oneline_whitespace.in
diff --git a/test/01_io/02_solved_oneline_whitespace.out b/test/010_io_H48/02_solved_oneline_whitespace.out
index dff224d..dff224d 100644
--- a/test/01_io/02_solved_oneline_whitespace.out
+++ b/test/010_io_H48/02_solved_oneline_whitespace.out
diff --git a/test/01_io/03_solved_multiline.in b/test/010_io_H48/03_solved_multiline.in
index 9f9652b..9f9652b 100644
--- a/test/01_io/03_solved_multiline.in
+++ b/test/010_io_H48/03_solved_multiline.in
diff --git a/test/01_io/03_solved_multiline.out b/test/010_io_H48/03_solved_multiline.out
index dff224d..dff224d 100644
--- a/test/01_io/03_solved_multiline.out
+++ b/test/010_io_H48/03_solved_multiline.out
diff --git a/test/01_io/04_unsolvable_ep.in b/test/010_io_H48/04_unsolvable_ep.in
index 8483962..8483962 100644
--- a/test/01_io/04_unsolvable_ep.in
+++ b/test/010_io_H48/04_unsolvable_ep.in
diff --git a/test/01_io/04_unsolvable_ep.out b/test/010_io_H48/04_unsolvable_ep.out
index e35c6e4..e35c6e4 100644
--- a/test/01_io/04_unsolvable_ep.out
+++ b/test/010_io_H48/04_unsolvable_ep.out
diff --git a/test/01_io/05_unsolvable_eo.in b/test/010_io_H48/05_unsolvable_eo.in
index a92089d..a92089d 100644
--- a/test/01_io/05_unsolvable_eo.in
+++ b/test/010_io_H48/05_unsolvable_eo.in
diff --git a/test/01_io/05_unsolvable_eo.out b/test/010_io_H48/05_unsolvable_eo.out
index e35c6e4..e35c6e4 100644
--- a/test/01_io/05_unsolvable_eo.out
+++ b/test/010_io_H48/05_unsolvable_eo.out
diff --git a/test/01_io/06_unsolvable_cp.in b/test/010_io_H48/06_unsolvable_cp.in
index 2085dd5..2085dd5 100644
--- a/test/01_io/06_unsolvable_cp.in
+++ b/test/010_io_H48/06_unsolvable_cp.in
diff --git a/test/01_io/06_unsolvable_cp.out b/test/010_io_H48/06_unsolvable_cp.out
index e35c6e4..e35c6e4 100644
--- a/test/01_io/06_unsolvable_cp.out
+++ b/test/010_io_H48/06_unsolvable_cp.out
diff --git a/test/01_io/07_unsolvable_co.in b/test/010_io_H48/07_unsolvable_co.in
index abd74e2..abd74e2 100644
--- a/test/01_io/07_unsolvable_co.in
+++ b/test/010_io_H48/07_unsolvable_co.in
diff --git a/test/01_io/07_unsolvable_co.out b/test/010_io_H48/07_unsolvable_co.out
index e35c6e4..e35c6e4 100644
--- a/test/01_io/07_unsolvable_co.out
+++ b/test/010_io_H48/07_unsolvable_co.out
diff --git a/test/01_io/08_unsolved.in b/test/010_io_H48/08_unsolved.in
index c992ce7..c992ce7 100644
--- a/test/01_io/08_unsolved.in
+++ b/test/010_io_H48/08_unsolved.in
diff --git a/test/01_io/08_unsolved.out b/test/010_io_H48/08_unsolved.out
index 8c132cf..8c132cf 100644
--- a/test/01_io/08_unsolved.out
+++ b/test/010_io_H48/08_unsolved.out
diff --git a/test/01_io/io_tests.c b/test/010_io_H48/io_H48_tests.c
index 913ec1d..7c3978e 100644
--- a/test/01_io/io_tests.c
+++ b/test/010_io_H48/io_H48_tests.c
@@ -13,14 +13,14 @@ int main() {
13 for (c = str; (*c = getchar()) != EOF; c++) ; 13 for (c = str; (*c = getchar()) != EOF; c++) ;
14 *c = '\0'; 14 *c = '\0';
15 15
16 cube = readcube(str); 16 cube = readcube(H48, str);
17 17
18 if (iserror(cube)) { 18 if (iserror(cube)) {
19 printf("Error reading cube\n"); 19 printf("Error reading cube\n");
20 } else if (!issolvable(cube)) { 20 } else if (!issolvable(cube)) {
21 printf("Cube is not solvable\n"); 21 printf("Cube is not solvable\n");
22 } else { 22 } else {
23 writecube(cube, str); 23 writecube(H48, cube, str);
24 printf("%s\n", str); 24 printf("%s\n", str);
25 } 25 }
26 26
diff --git a/test/011_io_SRC/01_solved.in b/test/011_io_SRC/01_solved.in
new file mode 100644
index 0000000..dff224d
--- /dev/null
+++ b/test/011_io_SRC/01_solved.in
@@ -0,0 +1 @@
UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0
diff --git a/test/011_io_SRC/01_solved.out b/test/011_io_SRC/01_solved.out
new file mode 100644
index 0000000..3853027
--- /dev/null
+++ b/test/011_io_SRC/01_solved.out
@@ -0,0 +1,4 @@
1{
2 .c = {0, 1, 2, 3, 4, 5, 6, 7},
3 .e = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
4}
diff --git a/test/011_io_SRC/io_SRC_tests.c b/test/011_io_SRC/io_SRC_tests.c
new file mode 100644
index 0000000..f454b6f
--- /dev/null
+++ b/test/011_io_SRC/io_SRC_tests.c
@@ -0,0 +1,28 @@
1#include <stdbool.h>
2#include <stdint.h>
3#include <stdio.h>
4
5#include "../../src/cube.h"
6
7#define STRLENMAX 10000
8
9int main() {
10 char *c, str[STRLENMAX];
11 cube_t cube;
12
13 for (c = str; (*c = getchar()) != EOF; c++) ;
14 *c = '\0';
15
16 cube = readcube(H48, str);
17
18 if (iserror(cube)) {
19 printf("Error reading cube\n");
20 } else if (!issolvable(cube)) {
21 printf("Cube is not solvable\n");
22 } else {
23 writecube(SRC, cube, str);
24 printf("%s\n", str);
25 }
26
27 return 0;
28}
diff --git a/test/02_move/move_tests.c b/test/02_move/move_tests.c
index 333c8ee..054466b 100644
--- a/test/02_move/move_tests.c
+++ b/test/02_move/move_tests.c
@@ -22,7 +22,7 @@ int main() {
22 } 22 }
23 23
24 fgets(str, STRLENMAX, stdin); 24 fgets(str, STRLENMAX, stdin);
25 cube = readcube(str); 25 cube = readcube(H48, str);
26 26
27 for (i = 0; i < n; i++) 27 for (i = 0; i < n; i++)
28 cube = move(cube, moves[i]); 28 cube = move(cube, moves[i]);
@@ -32,7 +32,7 @@ int main() {
32 } else if (!issolvable(cube)) { 32 } else if (!issolvable(cube)) {
33 printf("Moved cube is not solvable\n"); 33 printf("Moved cube is not solvable\n");
34 } else { 34 } else {
35 writecube(cube, str); 35 writecube(H48, cube, str);
36 printf("%s\n", str); 36 printf("%s\n", str);
37 } 37 }
38 38
diff --git a/test/03_inverse/inverse_tests.c b/test/03_inverse/inverse_tests.c
index c8d8d95..0a56568 100644
--- a/test/03_inverse/inverse_tests.c
+++ b/test/03_inverse/inverse_tests.c
@@ -11,7 +11,7 @@ int main() {
11 cube_t cube, inv; 11 cube_t cube, inv;
12 12
13 fgets(str, STRLENMAX, stdin); 13 fgets(str, STRLENMAX, stdin);
14 cube = readcube(str); 14 cube = readcube(H48, str);
15 inv = inverse(cube); 15 inv = inverse(cube);
16 16
17 if (iserror(inv)) { 17 if (iserror(inv)) {
@@ -19,7 +19,7 @@ int main() {
19 } else if (!issolvable(inv)) { 19 } else if (!issolvable(inv)) {
20 printf("Inverted cube is not solvable\n"); 20 printf("Inverted cube is not solvable\n");
21 } else { 21 } else {
22 writecube(inv, str); 22 writecube(H48, inv, str);
23 printf("%s\n", str); 23 printf("%s\n", str);
24 } 24 }
25 25
diff --git a/test/04_compose/compose_tests.c b/test/04_compose/compose_tests.c
index 575c2c9..a4a5f00 100644
--- a/test/04_compose/compose_tests.c
+++ b/test/04_compose/compose_tests.c
@@ -11,9 +11,9 @@ int main() {
11 cube_t c1, c2, c3; 11 cube_t c1, c2, c3;
12 12
13 fgets(str, STRLENMAX, stdin); 13 fgets(str, STRLENMAX, stdin);
14 c1 = readcube(str); 14 c1 = readcube(H48, str);
15 fgets(str, STRLENMAX, stdin); 15 fgets(str, STRLENMAX, stdin);
16 c2 = readcube(str); 16 c2 = readcube(H48, str);
17 17
18 c3 = compose(c1, c2); 18 c3 = compose(c1, c2);
19 19
@@ -22,7 +22,7 @@ int main() {
22 } else if (!issolvable(c3)) { 22 } else if (!issolvable(c3)) {
23 printf("Composed cube is not solvable\n"); 23 printf("Composed cube is not solvable\n");
24 } else { 24 } else {
25 writecube(c3, str); 25 writecube(H48, c3, str);
26 printf("%s\n", str); 26 printf("%s\n", str);
27 } 27 }
28 28
diff --git a/test/test.sh b/test/test.sh
index 5e17d33..9eae127 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -11,7 +11,7 @@ TESTOUT="test/last.out"
11TESTERR="test/last.err" 11TESTERR="test/last.err"
12CUBEOBJ="test/cube.o" 12CUBEOBJ="test/cube.o"
13 13
14$CC -c $SRC -o $CUBEOBJ 14$CC -c $SRC -o $CUBEOBJ || exit 1
15 15
16for t in test/*; do 16for t in test/*; do
17 if [ ! -d $t ]; then continue; fi 17 if [ ! -d $t ]; then continue; fi
diff --git a/utils/FORMAT.txt b/utils/FORMAT.txt
index 74e7442..091104c 100644
--- a/utils/FORMAT.txt
+++ b/utils/FORMAT.txt
@@ -1,4 +1,8 @@
1The functions readcube() and writecube() use the following format. 1The functions readcube() and writecube() use different formats to read
2and write a cube to text. Not all formats are supported for both input
3and output.
4
5# H48 - standard format for h48 (read, write)
2 6
3Each edge is represented by two letters denoting the sides it belongs to 7Each edge is represented by two letters denoting the sides it belongs to
4and one number denoting its orientation (0 oriented, 1 mis-oriented). 8and one number denoting its orientation (0 oriented, 1 mis-oriented).
@@ -21,4 +25,11 @@ The cube after the moves R'U'F looks like this:
21FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 25FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0
22UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 26UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0
23 27
24More formats might be supported in the future. 28# SRC - representation of the object in C code (write)
29
30The exact format depends on the internal cube representation. It is
31guaranteed that, if OUT is the output in this format, the line
32
33cube_t cube = OUT;
34
35is interpreted correctly by h48.

Generated with cgit - Back to sebastiano.tronto.net