OnceTest.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/private/SkOnce.h"
  8. #include "src/core/SkTaskGroup.h"
  9. #include "tests/Test.h"
  10. static void add_five(int* x) {
  11. *x += 5;
  12. }
  13. DEF_TEST(SkOnce_Singlethreaded, r) {
  14. int x = 0;
  15. // No matter how many times we do this, x will be 5.
  16. SkOnce once;
  17. once(add_five, &x);
  18. once(add_five, &x);
  19. once(add_five, &x);
  20. once(add_five, &x);
  21. once(add_five, &x);
  22. REPORTER_ASSERT(r, 5 == x);
  23. }
  24. DEF_TEST(SkOnce_Multithreaded, r) {
  25. int x = 0;
  26. // Run a bunch of tasks to be the first to add six to x.
  27. SkOnce once;
  28. SkTaskGroup().batch(1021, [&](int) {
  29. once([&] { x += 6; });
  30. });
  31. // Only one should have done the +=.
  32. REPORTER_ASSERT(r, 6 == x);
  33. }
  34. static int gX = 0;
  35. static void inc_gX() { gX++; }
  36. DEF_TEST(SkOnce_NoArg, r) {
  37. SkOnce once;
  38. once(inc_gX);
  39. once(inc_gX);
  40. once(inc_gX);
  41. REPORTER_ASSERT(r, 1 == gX);
  42. }