fold.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. if (!dojo._hasResource["dojox.lang.tests.fold"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.lang.tests.fold"] = true;
  6. dojo.provide("dojox.lang.tests.fold");
  7. dojo.require("dojox.lang.functional");
  8. (function() {
  9. var df = dojox.lang.functional, a = df.arg;
  10. tests.register("dojox.lang.tests.fold", [function testFoldl1(t) {
  11. t.assertEqual(df.foldl([1, 2, 3], "+", 0), 6);
  12. }, function testFoldl2(t) {
  13. t.assertEqual(df.foldl1([1, 2, 3], "*"), 6);
  14. }, function testFoldl3(t) {
  15. t.assertEqual(df.foldl1([1, 2, 3], "/"), 1 / 6);
  16. }, function testFoldl4(t) {
  17. t.assertEqual(df.foldl1([1, 2, 3], df.partial(
  18. Math.max, a, a)), 3);
  19. }, function testFoldl5(t) {
  20. t.assertEqual(df.foldl1([1, 2, 3], df.partial(
  21. Math.min, a, a)), 1);
  22. },
  23. function testFoldr1(t) {
  24. t.assertEqual(df.foldr([1, 2, 3], "+", 0), 6);
  25. }, function testFoldr2(t) {
  26. t.assertEqual(df.foldr1([1, 2, 3], "*"), 6);
  27. }, function testFoldr3(t) {
  28. t.assertEqual(df.foldr1([1, 2, 3], "/"), 3 / 2);
  29. }, function testFoldr4(t) {
  30. t.assertEqual(df.foldr1([1, 2, 3], df.partial(
  31. Math.max, a, a)), 3);
  32. }, function testFoldr5(t) {
  33. t.assertEqual(df.foldr1([1, 2, 3], df.partial(
  34. Math.min, a, a)), 1);
  35. },
  36. function testScanl1(t) {
  37. t.assertEqual(df.scanl([1, 2, 3], "+", 0), [0, 1,
  38. 3, 6]);
  39. }, function testScanl2(t) {
  40. t.assertEqual(df.scanl1([1, 2, 3], "*"), [1, 2, 6]);
  41. }, function testScanl3(t) {
  42. t
  43. .assertEqual(df.scanl1([1, 2, 3], df
  44. .partial(Math.max,
  45. a, a)), [1,
  46. 2, 3]);
  47. }, function testScanl4(t) {
  48. t
  49. .assertEqual(df.scanl1([1, 2, 3], df
  50. .partial(Math.min,
  51. a, a)), [1,
  52. 1, 1]);
  53. },
  54. function testScanr1(t) {
  55. t.assertEqual(df.scanr([1, 2, 3], "+", 0), [6, 5,
  56. 3, 0]);
  57. }, function testScanr2(t) {
  58. t.assertEqual(df.scanr1([1, 2, 3], "*"), [6, 6, 3]);
  59. }, function testScanr3(t) {
  60. t
  61. .assertEqual(df.scanr1([1, 2, 3], df
  62. .partial(Math.max,
  63. a, a)), [3,
  64. 3, 3]);
  65. }, function testScanr4(t) {
  66. t
  67. .assertEqual(df.scanr1([1, 2, 3], df
  68. .partial(Math.min,
  69. a, a)), [1,
  70. 2, 3]);
  71. }]);
  72. })();
  73. }