Stream.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "pch.h"
  2. #include <Cache.h>
  3. #include "CppUnitTest.h"
  4. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  5. namespace FrameworkTests
  6. {
  7. TEST_CLASS (StreamTests)
  8. {
  9. public:
  10. TEST_METHOD (EmtptyTest)
  11. {
  12. Framework::Array<int> array;
  13. array.stream().forEach([](int i) { Assert::Fail(); });
  14. }
  15. TEST_METHOD (FilterTest)
  16. {
  17. Framework::Array<int> array;
  18. array.add(1);
  19. array.add(2);
  20. array.add(4);
  21. array.add(5);
  22. array.add(7);
  23. int count = 0;
  24. array.stream()
  25. .filter([](int i) { return i % 2 == 0; })
  26. .forEach([&count](int i) { count++; });
  27. Assert::AreEqual(2, count);
  28. }
  29. TEST_METHOD (MapTest)
  30. {
  31. Framework::Array<int> array;
  32. array.add(1);
  33. array.add(2);
  34. array.add(4);
  35. array.add(5);
  36. array.add(7);
  37. Framework::Array<int>* result
  38. = array.stream()
  39. .map<int>([](int i) { return i * 2; })
  40. .collect<Framework::Array<int>*>(
  41. [](Framework::Array<int>* curr, int i) {
  42. curr->add(i);
  43. return curr;
  44. },
  45. new Framework::Array<int>());
  46. Assert::AreEqual(5, result->getEintragAnzahl());
  47. Assert::AreEqual(2, result->get(0));
  48. Assert::AreEqual(4, result->get(1));
  49. Assert::AreEqual(8, result->get(2));
  50. Assert::AreEqual(10, result->get(3));
  51. Assert::AreEqual(14, result->get(4));
  52. result->release();
  53. }
  54. TEST_METHOD (FlatMapTest)
  55. {
  56. Framework::RCArray<Framework::Array<int>> array;
  57. array.add(new Framework::Array<int>());
  58. array.add(new Framework::Array<int>());
  59. array.add(new Framework::Array<int>());
  60. array.z(1)->add(1);
  61. array.z(1)->add(2);
  62. array.z(1)->add(4);
  63. array.z(2)->add(5);
  64. array.z(2)->add(7);
  65. Framework::Array<int>* result
  66. = array.stream()
  67. .flatMap<int>([](Framework::Array<int>* val) {
  68. return val->stream();
  69. })
  70. .collect<Framework::Array<int>*>(
  71. [](Framework::Array<int>* curr, int i) {
  72. curr->add(i);
  73. return curr;
  74. },
  75. new Framework::Array<int>());
  76. Assert::AreEqual(5, result->getEintragAnzahl());
  77. Assert::AreEqual(1, result->get(0));
  78. Assert::AreEqual(2, result->get(1));
  79. Assert::AreEqual(4, result->get(2));
  80. Assert::AreEqual(5, result->get(3));
  81. Assert::AreEqual(7, result->get(4));
  82. result->release();
  83. }
  84. };
  85. } // namespace FrameworkTests