1 // The MIT License (MIT)
2 //
3 // 	Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev
4 //
5 // 	Permission is hereby granted, free of charge, to any person obtaining a copy
6 // 	of this software and associated documentation files (the "Software"), to deal
7 // 	in the Software without restriction, including without limitation the rights
8 // 	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // 	copies of the Software, and to permit persons to whom the Software is
10 // 	furnished to do so, subject to the following conditions:
11 //
12 //  The above copyright notice and this permission notice shall be included in
13 // 	all copies or substantial portions of the Software.
14 //
15 // 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // 	THE SOFTWARE.
22 
23 #include "Tests.h"
24 #include <UnitTest++.h>
25 #include <MTScheduler.h>
26 #include <math.h> // for sin/cos
27 
28 SUITE(GroupTests)
29 {
30 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
31 namespace SimpleWaitFromSubtask
32 {
33 	MT::Atomic32<int32> subtaskCount(0);
34 	MT::Atomic32<int32> animTaskCount(0);
35 	MT::Atomic32<int32> physTaskCount(0);
36 
37 
38 	struct DummySubTask
39 	{
40 		MT_DECLARE_TASK(DummySubTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue);
41 
42 		float tempData[64];
43 
44 		void Do(MT::FiberContext& )
45 		{
46 			for (size_t i = 0; i < MT_ARRAY_SIZE(tempData); i++)
47 			{
48 				tempData[i] = cos((float)rand() / RAND_MAX) + sin((float)rand() / RAND_MAX);
49 			}
50 
51 			subtaskCount.IncFetch();
52 		}
53 	};
54 
55 
56 	struct DummyAnimTask
57 	{
58 		MT_DECLARE_TASK(DummyAnimTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue);
59 
60 		float tempData[128];
61 
62 		void Do(MT::FiberContext& ctx)
63 		{
64 			for (size_t i = 0; i < MT_ARRAY_SIZE(tempData); i++)
65 			{
66 				tempData[i] = cos((float)rand() / RAND_MAX);
67 			}
68 
69 			DummySubTask subtasks[16];
70 			ctx.RunSubtasksAndYield(MT::TaskGroup::Default(), &subtasks[0], MT_ARRAY_SIZE(subtasks));
71 
72 			animTaskCount.IncFetch();
73 		}
74 	};
75 
76 
77 	struct DummyPhysicTask
78 	{
79 		MT_DECLARE_TASK(DummyPhysicTask, MT::StackRequirements::STANDARD, MT::TaskPriority::NORMAL, MT::Color::Blue);
80 
81 		float tempData[128];
82 
83 		void Do(MT::FiberContext& ctx)
84 		{
85 			for (size_t i = 0; i < MT_ARRAY_SIZE(tempData); i++)
86 			{
87 				tempData[i] = sin((float)rand() / RAND_MAX);
88 			}
89 
90 			DummySubTask subtasks[8];
91 			ctx.RunSubtasksAndYield(MT::TaskGroup::Default(), &subtasks[0], MT_ARRAY_SIZE(subtasks));
92 
93 			physTaskCount.IncFetch();
94 		}
95 	};
96 
97 
98 
99 	TEST(RunGroupTests)
100 	{
101 		static uint32 waitTime = 50000;
102 
103 		subtaskCount.Store(0);
104 		animTaskCount.Store(0);
105 		physTaskCount.Store(0);
106 
107 		MT::TaskScheduler scheduler;
108 
109 		MT::TaskGroup groupEmpty = scheduler.CreateGroup();
110 		MT::TaskGroup groupAnim = scheduler.CreateGroup();
111 
112 
113 		bool emptyWaitRes0 = scheduler.WaitGroup(groupEmpty, 200);
114 		CHECK(emptyWaitRes0 == true);
115 
116 		DummyAnimTask dummyAnim[4];
117 		scheduler.RunAsync(groupAnim, &dummyAnim[0], MT_ARRAY_SIZE(dummyAnim));
118 
119 		MT::TaskGroup groupPhysic = scheduler.CreateGroup();
120 		DummyPhysicTask dummyPhysic[4];
121 		scheduler.RunAsync(groupPhysic, &dummyPhysic[0], MT_ARRAY_SIZE(dummyPhysic));
122 
123 		bool emptyWaitRes1 = scheduler.WaitGroup(groupEmpty, 20);
124 		CHECK(emptyWaitRes1 == true);
125 
126 		CHECK(scheduler.WaitGroup(groupAnim, waitTime));
127 
128 		bool emptyWaitRes2 = scheduler.WaitGroup(groupEmpty, 200);
129 		CHECK(emptyWaitRes2 == true);
130 
131 
132 		CHECK_EQUAL(4, animTaskCount.Load());
133 		CHECK(subtaskCount.Load() >= 4 * 16);
134 
135 
136 		CHECK(scheduler.WaitGroup(groupPhysic, waitTime));
137 
138 		CHECK_EQUAL(4, animTaskCount.Load());
139 		CHECK_EQUAL(4, physTaskCount.Load());
140 		CHECK_EQUAL(96, subtaskCount.Load());
141 	}
142 }
143 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
144 }
145 
146