legacy_future_ut.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "legacy_future.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NThreading {
  4. Y_UNIT_TEST_SUITE(TLegacyFutureTest) {
  5. int intf() {
  6. return 17;
  7. }
  8. Y_UNIT_TEST(TestIntFunction) {
  9. TLegacyFuture<int> f((&intf));
  10. UNIT_ASSERT_VALUES_EQUAL(17, f.Get());
  11. }
  12. static int r;
  13. void voidf() {
  14. r = 18;
  15. }
  16. Y_UNIT_TEST(TestVoidFunction) {
  17. r = 0;
  18. TLegacyFuture<> f((&voidf));
  19. f.Get();
  20. UNIT_ASSERT_VALUES_EQUAL(18, r);
  21. }
  22. struct TSampleClass {
  23. int mValue;
  24. TSampleClass(int value)
  25. : mValue(value)
  26. {
  27. }
  28. int Calc() {
  29. return mValue + 1;
  30. }
  31. };
  32. Y_UNIT_TEST(TestMethod) {
  33. TLegacyFuture<int> f11(std::bind(&TSampleClass::Calc, TSampleClass(3)));
  34. UNIT_ASSERT_VALUES_EQUAL(4, f11.Get());
  35. TLegacyFuture<int> f12(std::bind(&TSampleClass::Calc, TSampleClass(3)), SystemThreadFactory());
  36. UNIT_ASSERT_VALUES_EQUAL(4, f12.Get());
  37. TSampleClass c(5);
  38. TLegacyFuture<int> f21(std::bind(&TSampleClass::Calc, std::ref(c)));
  39. UNIT_ASSERT_VALUES_EQUAL(6, f21.Get());
  40. TLegacyFuture<int> f22(std::bind(&TSampleClass::Calc, std::ref(c)), SystemThreadFactory());
  41. UNIT_ASSERT_VALUES_EQUAL(6, f22.Get());
  42. }
  43. struct TSomeThreadPool: public IThreadFactory {};
  44. Y_UNIT_TEST(TestFunction) {
  45. std::function<int()> f((&intf));
  46. UNIT_ASSERT_VALUES_EQUAL(17, TLegacyFuture<int>(f).Get());
  47. UNIT_ASSERT_VALUES_EQUAL(17, TLegacyFuture<int>(f, SystemThreadFactory()).Get());
  48. if (false) {
  49. TSomeThreadPool* q = nullptr;
  50. TLegacyFuture<int>(f, q); // just check compiles, do not start
  51. }
  52. }
  53. }
  54. }