0e34ea7a2d6ef301c66b07f920e85656c448fcea
[yaffs2.git] / direct / timothy_tests / quick_tests / quick_tests.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Timothy Manning <timothy@yaffs.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include "quick_tests.h"
15
16
17 int random_seed;
18 int simulate_power_failure = 0;
19
20
21
22
23 static int number_of_random_tests=0;
24 static unsigned int num_of_tests_pass=0;
25 static unsigned int num_of_tests_failed=0;
26 static unsigned int total_number_of_tests=(sizeof(test_list)/sizeof(test_template));
27
28 int main(int argc, char *argv[])
29 {
30         int x=0;
31
32         init_quick_tests(argc, argv);
33         logical_run_of_tests();
34         for (x=0;x<number_of_random_tests;x ++){
35                 run_random_test_loop();
36         }       
37         /*this is where the loop should break to*/
38         quit_quick_tests(0);
39 }
40
41
42 void run_random_test_loop(void)
43 {
44         int id=0;
45         int x=0;
46         int run_list[total_number_of_tests];
47         for (x=0;x<total_number_of_tests;x++){ 
48                 id = (rand() % (total_number_of_tests-1));
49                 run_test(id);   
50         }
51 }
52
53 void logical_run_of_tests(void)
54 {
55         unsigned int x=0;
56         print_message("\n\nrunning quick tests for yaffs\n\n", 0);
57
58         for (x=0;x<total_number_of_tests;x++){
59                 run_test(x);
60         }
61 }
62
63 void run_test(int x)
64 {
65         int output=0;
66         char message[200];
67         message[0]='\0';
68
69         yaffs_set_error(0);     /*reset the last error to 0 */
70         //printf("foo exists %d\n",test_yaffs_open());
71         sprintf(message,"\nrunning test: %s \n",test_list[x].name_of_test);
72         print_message(message,3);
73         output=test_list[x].p_function();       /*run test*/
74         if (output>=0){
75                 /*test has passed*/
76                 sprintf(message,"\ttest %s passed\n",test_list[x].name_of_test);
77                 print_message(message,3); 
78                 num_of_tests_pass++;
79         } else {
80                 /*test is assumed to have failed*/
81                 //printf("test failed\n");
82                 sprintf(message,"test: %s failed\n",test_list[x].name_of_test);
83                 print_message(message,1);               
84                 num_of_tests_failed ++; 
85         
86                 get_error();
87                 print_message("\n\n",1);
88                 if (get_exit_on_error()){       
89                         quit_quick_tests(1);
90                 }
91         }
92         output=0;
93         output=test_list[x].p_function_clean(); /*clean the test*/
94         if (output <0){
95                 /* if the test failed to clean it's self then */
96                 sprintf(message,"test: %s failed to clean\n",test_list[x].name_of_test);
97                 print_message(message,1);               
98                 num_of_tests_failed ++; 
99                 num_of_tests_pass--;
100                 get_error();
101                 printf("\n\n");
102                 if (get_exit_on_error()){
103                         quit_quick_tests(1);
104                 }
105                 
106         } else {
107                 sprintf(message,"\ttest clean: %s passed\n",test_list[x].name_of_test);
108                 print_message(message,3);
109         }
110 }
111
112 void quit_quick_tests(int exit_code)
113 {
114         char message[30];
115         message[0]='\0';        
116         if (num_of_tests_pass==total_number_of_tests &&  num_of_tests_failed==0){
117                 printf("\t OK --all tests passed\n");
118         }
119         printf("out of %d tests, %d ran: %d passed and %d failed\n\n\n", total_number_of_tests,(num_of_tests_pass+num_of_tests_failed) ,num_of_tests_pass,num_of_tests_failed);
120         yaffs_unmount(YAFFS_MOUNT_POINT);
121         exit(exit_code);
122 }
123
124 void get_error(void)
125 {
126         int error_code=0;
127         char message[30];
128         message[0]='\0';
129
130         error_code=yaffs_get_error();
131         sprintf(message,"yaffs_error code %d\n",error_code);
132         print_message(message,1);
133         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
134         print_message(message,1);
135 }
136
137 void init_quick_tests(int argc, char *argv[])
138 {
139         int trace=0;
140         int x=0;        
141         for (x = 0; x < argc; x++){
142                 if (0==strcmp(argv[x],"-h")){
143                         printf("help\n");
144                         printf("-h will print the commands available\n");
145                         printf("-c will continue after a test failes else the program will exit\n");
146                         printf("-v will print all messages\n");
147                         printf("-q quiet mode only the number of tests passed and failed will be printed\n");
148                         printf("-t [number] set yaffs_trace to number\n");
149                         printf("-r [number] sets the number of random loops to run after the the test has run\n");
150                         exit(0);
151                 } else if (0==strcmp(argv[x],"-c")) {
152                         set_exit_on_error(0);
153                 } else if (0==strcmp(argv[x],"-q")) {
154                         set_print_level(-3);
155                 } else if (0==strcmp(argv[x],"-t")) {
156                         trace = atoi(argv[x+1]);
157                 }  else if (0==strcmp(argv[x],"-v")) {
158                         set_print_level(5);
159                 } else if (0==strcmp(argv[x],"-r")) {
160                         number_of_random_tests=atoi(argv[x+1]);
161                 }
162
163         }
164         yaffs_start_up();
165         yaffs_set_trace(trace);
166
167 }