tests.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #define FUNC_CHECK_ARR(%0,%1) for (new __i, __j = min(sizeof (%0), sizeof (%1)); __i != __j; ++__i) ASSERT(%0[__i] == %1[__i])
  2. Test:FUNC_Map1()
  3. {
  4. new
  5. a0[10] = {0, 1, 2, ...},
  6. a1[10] = {0, 2, 4, ...};
  7. inline const Double(x)
  8. {
  9. @return x * 2;
  10. }
  11. Map(using inline Double, a0, a0);
  12. FUNC_CHECK_ARR(a0, a1);
  13. }
  14. Test:FUNC_Map2()
  15. {
  16. new
  17. a0[10] = {0, 1, 2, ...},
  18. a1[10] = {0, 2, 4, ...},
  19. a2[10] = {0, 1, 2, ...},
  20. a3[10];
  21. inline const Double(x)
  22. {
  23. @return x * 2;
  24. }
  25. Map(using inline Double, a0, a3);
  26. FUNC_CHECK_ARR(a0, a2);
  27. FUNC_CHECK_ARR(a3, a1);
  28. }
  29. Test:FUNC_Map3()
  30. {
  31. new
  32. a0[10] = {0, 1, 2, ...},
  33. a1[10] = {1, 4, 7, ...},
  34. a2[10] = {0, 1, 2, ...},
  35. a3[10];
  36. Map({_0 * 3 + 1}, a0, a3);
  37. FUNC_CHECK_ARR(a0, a2);
  38. FUNC_CHECK_ARR(a3, a1);
  39. }
  40. Test:FUNC_Map_1()
  41. {
  42. new
  43. a0[10] = {0, 1, 2, ...},
  44. a1[10] = {0, 1, 2, ...};
  45. inline const Triple(x)
  46. {
  47. @return x * 3;
  48. }
  49. Map_(using inline Triple, a0);
  50. FUNC_CHECK_ARR(a0, a1);
  51. }
  52. Test:FUNC_Map_2()
  53. {
  54. new
  55. a0[10] = {0, 1, 2, ...},
  56. a1[10] = {0, 1, 2, ...};
  57. Map_({_0 * 4}, a0);
  58. FUNC_CHECK_ARR(a0, a1);
  59. }
  60. Test:FUNC_MapIdx()
  61. {
  62. new
  63. a0[10] = {1, ...},
  64. a1[10] = {2, 4, 6, ...};
  65. inline const AddAndMul(idx, x)
  66. {
  67. @return (x + idx) * 2;
  68. }
  69. MapIdx(using inline AddAndMul, a0, a0);
  70. FUNC_CHECK_ARR(a0, a1);
  71. }
  72. Test:FUNC_MapIdx_()
  73. {
  74. new
  75. a0[10] = {0, 1, 2, ...},
  76. a1[10] = {0, 1, 2, ...};
  77. inline const Thing(idx, x)
  78. {
  79. #pragma unused idx, x
  80. @return 42;
  81. }
  82. MapIdx_(using inline Thing, a0);
  83. FUNC_CHECK_ARR(a0, a1);
  84. }
  85. Test:FUNC_ZipWith()
  86. {
  87. new
  88. a0[10] = { 0, 1, 2, ...},
  89. a1[10] = {10, 20, 30, ...},
  90. a2[10],
  91. a3[10] = { 0, 20, 60, 120, 200, 300, 420, 560, 720, 900};
  92. inline const Mul(a, b) @return a * b;
  93. ZipWith(using inline Mul, a0, a1, a2);
  94. FUNC_CHECK_ARR(a2, a3);
  95. }
  96. Test:FUNC_ZipWith3()
  97. {
  98. new
  99. a0[10] = { 0, 1, 2, ...},
  100. a1[10] = {10, 20, 30, ...},
  101. a9[10] = {22, ...},
  102. a2[10],
  103. a3[10] = {22, 42, 82, 142, 222, 322, 442, 582, 742, 922};
  104. inline const MulAdd(a, b, c) @return a * b + c;
  105. ZipWith3(using inline MulAdd, a0, a1, a9, a2);
  106. FUNC_CHECK_ARR(a2, a3);
  107. }
  108. Test:FUNC_FoldR1()
  109. {
  110. new
  111. a0[10] = { 0, 1, 2, ...},
  112. ret;
  113. ret = FoldR({_0 * _1}, a0, 10);
  114. ASSERT(ret == 0);
  115. }
  116. Test:FUNC_FoldR2()
  117. {
  118. new
  119. a0[10] = { 1, 2, 3, ...},
  120. ret;
  121. ret = FoldR({_0 * _1}, a0, 10);
  122. ASSERT(ret == 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 10);
  123. }
  124. Test:FUNC_FoldL1()
  125. {
  126. new
  127. a0[10] = { 1, 2, 3, ...},
  128. ret;
  129. ret = FoldL({_0 * _1}, 11, a0);
  130. ASSERT(ret == 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11);
  131. }
  132. Test:FUNC_FoldR3()
  133. {
  134. new
  135. a0[10] = { 1, 2, 3, ...},
  136. ret;
  137. ret = FoldR({_0 * _1}, a0, 15, 0);
  138. ASSERT(ret == 15);
  139. }
  140. Test:FUNC_FoldL2()
  141. {
  142. new
  143. a0[10] = { 1, 2, 3, ...},
  144. ret;
  145. ret = FoldL({_0 * _1}, 99, a0, 0);
  146. ASSERT(ret == 99);
  147. }
  148. #undef FUNC_CHECK_ARR