Flutter Engine
The Flutter Engine
OnceTest.cpp
Go to the documentation of this file.
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
10#include "tests/Test.h"
11
12#include <functional>
13
14static void add_five(int* x) {
15 *x += 5;
16}
17
18DEF_TEST(SkOnce_Singlethreaded, r) {
19 int x = 0;
20
21 // No matter how many times we do this, x will be 5.
22 SkOnce once;
23 once(add_five, &x);
24 once(add_five, &x);
25 once(add_five, &x);
26 once(add_five, &x);
27 once(add_five, &x);
28
29 REPORTER_ASSERT(r, 5 == x);
30}
31
32DEF_TEST(SkOnce_Multithreaded, r) {
33 int x = 0;
34
35 // Run a bunch of tasks to be the first to add six to x.
36 SkOnce once;
37 SkTaskGroup().batch(1021, [&](int) {
38 once([&] { x += 6; });
39 });
40
41 // Only one should have done the +=.
42 REPORTER_ASSERT(r, 6 == x);
43}
44
45static int gX = 0;
46static void inc_gX() { gX++; }
47
48DEF_TEST(SkOnce_NoArg, r) {
49 SkOnce once;
50 once(inc_gX);
51 once(inc_gX);
52 once(inc_gX);
53 REPORTER_ASSERT(r, 1 == gX);
54}
static void inc_gX()
Definition: OnceTest.cpp:46
DEF_TEST(SkOnce_Singlethreaded, r)
Definition: OnceTest.cpp:18
static int gX
Definition: OnceTest.cpp:45
static void add_five(int *x)
Definition: OnceTest.cpp:14
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
Definition: SkOnce.h:22
void batch(int N, std::function< void(int)> fn)
Definition: SkTaskGroup.cpp:24
double x