/specs/runtime/switch.ds

http://github.com/wilkie/djehuty · Unknown · 122 lines · 104 code · 18 blank · 0 comment · 0 complexity · d93b72cd7716b0723c6cf8e2dd4de07c MD5 · raw file

  1. module specs.runtime.switch;
  2. import core.util;
  3. import math.random;
  4. describe runtime() {
  5. describe _d_switch_string {
  6. it should_handle_empty_switch_statements {
  7. string foo = "hello";
  8. switch(foo) {
  9. default:
  10. break;
  11. }
  12. }
  13. it should_handle_one_case {
  14. string foo = "hello";
  15. switch(foo) {
  16. case "hello":
  17. should(true);
  18. break;
  19. default:
  20. should(false);
  21. break;
  22. }
  23. }
  24. it should_handle_three_cases {
  25. string foo = "hello";
  26. switch(foo) {
  27. case "abc":
  28. case "zzt":
  29. should(false);
  30. break;
  31. case "hello":
  32. should(true);
  33. break;
  34. default:
  35. should(false);
  36. break;
  37. }
  38. }
  39. template StringList(int idx) {
  40. static if (idx == 50) {
  41. const char[] StringList = `"` ~ IntToStr!(idx, 16) ~ `"
  42. `;
  43. }
  44. else {
  45. const char[] StringList = `"` ~ IntToStr!(idx, 16) ~ `",
  46. ` ~ StringList!(idx+1);
  47. }
  48. }
  49. template StringArray() {
  50. const char[] StringArray = `
  51. string[] foo = [
  52. ` ~ StringList!(0) ~ `
  53. ];`;
  54. }
  55. template CaseList(int idx) {
  56. static if (idx == 50) {
  57. const char[] CaseList = `case "` ~ IntToStr!(idx, 16) ~ `":
  58. picked = "` ~ IntToStr!(idx, 16) ~ `";
  59. break;`;
  60. }
  61. else {
  62. const char[] CaseList = `case "` ~ IntToStr!(idx, 16) ~ `":
  63. picked = "` ~ IntToStr!(idx, 16) ~ `";
  64. break;
  65. ` ~ CaseList!(idx+1);
  66. }
  67. }
  68. template SwitchList() {
  69. const char[] SwitchList = `
  70. switch(foo[idx]) {
  71. ` ~ CaseList!(0) ~ `
  72. default:
  73. picked = "";
  74. break;
  75. }`;
  76. }
  77. it should_handle_many_cases {
  78. mixin(StringArray!());
  79. auto r = new Random();
  80. for (size_t i=0; i<50; i++) {
  81. size_t idx = cast(size_t)r.nextLong(50);
  82. string picked;
  83. mixin(SwitchList!());
  84. should(picked == foo[idx]);
  85. }
  86. }
  87. it should_handle_empty_string {
  88. switch("") {
  89. case "":
  90. should(true);
  91. break;
  92. case "abc":
  93. case "zsdf":
  94. case "asdf":
  95. case "afsfdfas":
  96. should(false);
  97. break;
  98. default:
  99. should(false);
  100. break;
  101. }
  102. }
  103. }
  104. }