Refactor and automaize the test.
[bertos.git] / bertos / kern / proc_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Test kernel process.
34  *
35  * \version $Id$
36  * \author Daniele Basile <asterix@develer.com>
37  */
38
39 #include <kern/proc.h>
40 #include <kern/irq.h>
41 #include <kern/monitor.h>
42
43 #include <drv/timer.h>
44 #include <cfg/test.h>
45
46
47 // Global settings for the test.
48 #define MAX_GLOBAL_COUNT             1024
49 #define TEST_TIME_OUT_MS             6000
50 #define DELAY                           5
51
52 // Settings for the test process.
53 //Process 1
54 #define INC_PROC_T1                     1
55 #define DELAY_PROC_T1   INC_PROC_T1*DELAY
56 //Process 2
57 #define INC_PROC_T2                     3
58 #define DELAY_PROC_T2   INC_PROC_T2*DELAY
59 //Process 3
60 #define INC_PROC_T3                     5
61 #define DELAY_PROC_T3   INC_PROC_T3*DELAY
62 //Process 4
63 #define INC_PROC_T4                     7
64 #define DELAY_PROC_T4   INC_PROC_T4*DELAY
65 //Process 5
66 #define INC_PROC_T5                    11
67 #define DELAY_PROC_T5   INC_PROC_T5*DELAY
68 //Process 6
69 #define INC_PROC_T6                    13
70 #define DELAY_PROC_T6   INC_PROC_T6*DELAY
71 //Process 7
72 #define INC_PROC_T7                    17
73 #define DELAY_PROC_T7   INC_PROC_T7*DELAY
74 //Process 8
75 #define INC_PROC_T8                    19
76 #define DELAY_PROC_T8   INC_PROC_T8*DELAY
77
78 //Global count for each process.
79 unsigned int t1_count = 0;
80 unsigned int t2_count = 0;
81 unsigned int t3_count = 0;
82 unsigned int t4_count = 0;
83 unsigned int t5_count = 0;
84 unsigned int t6_count = 0;
85 unsigned int t7_count = 0;
86 unsigned int t8_count = 0;
87
88 /*
89  * These macros generate the code needed to create the test process functions.
90  */
91 #define PROC_TEST(num) static void proc_test##num(void) \
92 { \
93         unsigned int local_count = 0; \
94         \
95         for (int i = 0; i < INC_PROC_T##num; ++i) \
96         { \
97                 t##num##_count++; \
98                 kprintf("> Process[%d]: count[%d]\n", num, t##num##_count); \
99                 timer_delay(DELAY_PROC_T##num); \
100         } \
101 } \
102
103 #define PROC_TEST_STACK(num)  static cpu_stack_t proc_test##num##_stack[CONFIG_KERN_MINSTACKSIZE / sizeof(cpu_stack_t)];
104 #define PROC_TEST_INIT(num)   proc_new(proc_test##num, NULL, sizeof(proc_test##num##_stack), proc_test##num##_stack);
105
106 // Define process
107 PROC_TEST(1)
108 PROC_TEST(2)
109 PROC_TEST(3)
110 PROC_TEST(4)
111 PROC_TEST(5)
112 PROC_TEST(6)
113 PROC_TEST(7)
114 PROC_TEST(8)
115
116 // Define process stacks for test.
117 PROC_TEST_STACK(1)
118 PROC_TEST_STACK(2)
119 PROC_TEST_STACK(3)
120 PROC_TEST_STACK(4)
121 PROC_TEST_STACK(5)
122 PROC_TEST_STACK(6)
123 PROC_TEST_STACK(7)
124 PROC_TEST_STACK(8)
125
126
127 /**
128  * Process scheduling test
129  */
130 int proc_testRun(void)
131 {
132         kprintf("Run Process test..\n");
133
134         //Init the process tests
135         PROC_TEST_INIT(1)
136         PROC_TEST_INIT(2)
137         PROC_TEST_INIT(3)
138         PROC_TEST_INIT(4)
139         PROC_TEST_INIT(5)
140         PROC_TEST_INIT(6)
141         PROC_TEST_INIT(7)
142         PROC_TEST_INIT(8)
143         kputs("> Main: Processes created\n");
144
145         for (int i = 0; i < 30; ++i)
146         {
147                 kputs("> Main\n");
148                 timer_delay(93);
149                 monitor_report();
150         }
151
152         if( t1_count == INC_PROC_T1 &&
153                 t2_count == INC_PROC_T2 &&
154                 t3_count == INC_PROC_T3 &&
155                 t4_count == INC_PROC_T4 &&
156                 t5_count == INC_PROC_T5 &&
157                 t6_count == INC_PROC_T6 &&
158                 t7_count == INC_PROC_T7 &&
159                 t8_count == INC_PROC_T8)
160         {
161                 kputs("> Main: process test finished..ok!\n");
162                 return 0;
163         }
164
165         kputs("> Main: process test..fail!\n");
166         return -1;
167 }
168
169
170 int proc_testSetup(void)
171 {
172         kdbg_init();
173
174         #if CONFIG_KERN_PREEMPT
175                 kprintf("Init Interrupt (preempt mode)..");
176                 irq_init();
177                 kprintf("Done.\n");
178         #endif
179
180         kprintf("Init Timer..");
181         timer_init();
182         kprintf("Done.\n");
183
184         kprintf("Init Process..");
185         proc_init();
186         kprintf("Done.\n");
187
188         return 0;
189 }
190
191 int proc_testTearDown(void)
192 {
193         kputs("TearDown Process test.\n");
194         return 0;
195 }
196
197 TEST_MAIN(proc);