latin-test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include "puzzles.h"
  5. #include "latin.h"
  6. static const char *quis;
  7. #define ELT(sq,x,y) (sq[((y)*order)+(x)])
  8. static void latin_print(digit *sq, int order)
  9. {
  10. int x, y;
  11. for (y = 0; y < order; y++) {
  12. for (x = 0; x < order; x++) {
  13. printf("%2u ", ELT(sq, x, y));
  14. }
  15. printf("\n");
  16. }
  17. printf("\n");
  18. }
  19. static void gen(int order, random_state *rs, int debug)
  20. {
  21. digit *sq;
  22. sq = latin_generate(order, rs);
  23. latin_print(sq, order);
  24. if (latin_check(sq, order)) {
  25. fprintf(stderr, "Square is not a latin square!");
  26. exit(1);
  27. }
  28. sfree(sq);
  29. }
  30. static void test_soak(int order, random_state *rs)
  31. {
  32. digit *sq;
  33. int n = 0;
  34. time_t tt_start, tt_now, tt_last;
  35. tt_now = tt_start = time(NULL);
  36. while(1) {
  37. sq = latin_generate(order, rs);
  38. sfree(sq);
  39. n++;
  40. tt_last = time(NULL);
  41. if (tt_last > tt_now) {
  42. tt_now = tt_last;
  43. printf("%d total, %3.1f/s\n", n,
  44. (double)n / (double)(tt_now - tt_start));
  45. }
  46. }
  47. }
  48. static void usage_exit(const char *msg)
  49. {
  50. if (msg)
  51. fprintf(stderr, "%s: %s\n", quis, msg);
  52. fprintf(stderr, "Usage: %s [--seed SEED] --soak <params> | [game_id [game_id ...]]\n", quis);
  53. exit(1);
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. int i, soak = 0;
  58. random_state *rs;
  59. time_t seed = time(NULL);
  60. quis = argv[0];
  61. while (--argc > 0) {
  62. const char *p = *++argv;
  63. if (!strcmp(p, "--soak"))
  64. soak = 1;
  65. else if (!strcmp(p, "--seed")) {
  66. if (argc == 0)
  67. usage_exit("--seed needs an argument");
  68. seed = (time_t)atoi(*++argv);
  69. argc--;
  70. } else if (*p == '-')
  71. usage_exit("unrecognised option");
  72. else
  73. break; /* finished options */
  74. }
  75. rs = random_new((void*)&seed, sizeof(time_t));
  76. if (soak == 1) {
  77. if (argc != 1) usage_exit("only one argument for --soak");
  78. test_soak(atoi(*argv), rs);
  79. } else {
  80. if (argc > 0) {
  81. for (i = 0; i < argc; i++) {
  82. gen(atoi(*argv++), rs, 1);
  83. }
  84. } else {
  85. while (1) {
  86. i = random_upto(rs, 20) + 1;
  87. gen(i, rs, 0);
  88. }
  89. }
  90. }
  91. random_free(rs);
  92. return 0;
  93. }