Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[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
29 const struct option long_options[]={
30         {"help",        0,NULL,'h'},
31         {"quiet",       0,NULL,'q'},
32         {"number",      1,NULL,'n'},
33         {"trace",       1,NULL,'t'},
34         {"continue",    0,NULL,'c'},
35         {"verbose",     0,NULL,'v'}
36 };
37
38 const char short_options[]="hqn:t:cv";
39
40
41
42 int main(int argc, char *argv[])
43 {
44         int x=0;
45
46         init_quick_tests(argc, argv);
47         logical_run_of_tests();
48         for (x=0;x<number_of_random_tests;x ++){
49                 run_random_test_loop();
50         }       
51         /*this is where the loop should break to*/
52         quit_quick_tests(0);
53 }
54
55
56 void run_random_test_loop(void)
57 {
58         int id=0;
59         int x=0;
60         int run_list[total_number_of_tests];
61         for (x=0;x<total_number_of_tests;x++){ 
62                 id = (rand() % (total_number_of_tests-1));
63                 run_test(id);   
64         }
65 }
66
67 void logical_run_of_tests(void)
68 {
69         unsigned int x=0;
70         print_message("\n\nrunning quick tests for yaffs\n\n", 0);
71
72         for (x=0;x<total_number_of_tests;x++){
73                 run_test(x);
74         }
75 }
76
77 void run_test(int x)
78 {
79         int output=0;
80         char message[200];
81         message[0]='\0';
82
83         yaffs_set_error(0);     /*reset the last error to 0 */
84         //printf("foo exists %d\n",test_yaffs_open());
85         sprintf(message,"\nrunning test: %s \n",test_list[x].name_of_test);
86         print_message(message,3);
87         output=test_list[x].p_function();       /*run test*/
88         if (output>=0){
89                 /*test has passed*/
90                 sprintf(message,"\ttest %s passed\n",test_list[x].name_of_test);
91                 print_message(message,3); 
92                 num_of_tests_pass++;
93         } else {
94                 /*test is assumed to have failed*/
95                 //printf("test failed\n");
96                 sprintf(message,"test: %s failed\n",test_list[x].name_of_test);
97                 print_message(message,1);               
98                 num_of_tests_failed ++; 
99         
100                 get_error();
101                 print_message("\n\n",1);
102                 if (get_exit_on_error()){       
103                         quit_quick_tests(1);
104                 }
105         }
106         output=0;
107         output=test_list[x].p_function_clean(); /*clean the test*/
108         if (output <0){
109                 /* if the test failed to clean it's self then */
110                 sprintf(message,"test: %s failed to clean\n",test_list[x].name_of_test);
111                 print_message(message,1);               
112                 num_of_tests_failed ++; 
113                 num_of_tests_pass--;
114                 get_error();
115                 printf("\n\n");
116                 if (get_exit_on_error()){
117                         quit_quick_tests(1);
118                 }
119                 
120         } else {
121                 sprintf(message,"\ttest clean: %s passed\n",test_list[x].name_of_test);
122                 print_message(message,3);
123         }
124 }
125
126 void quit_quick_tests(int exit_code)
127 {
128         char message[30];
129         message[0]='\0';        
130         if (num_of_tests_pass==total_number_of_tests &&  num_of_tests_failed==0){
131                 printf("\t OK --all tests passed\n");
132         }
133         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);
134         yaffs_unmount(YAFFS_MOUNT_POINT);
135         exit(exit_code);
136 }
137
138 void get_error(void)
139 {
140         int error_code=0;
141         char message[30];
142         message[0]='\0';
143
144         error_code=yaffs_get_error();
145         sprintf(message,"yaffs_error code %d\n",error_code);
146         print_message(message,1);
147         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
148         print_message(message,1);
149 }
150
151 void init_quick_tests(int argc, char *argv[])
152 {
153         int trace=0;
154         int new_option;
155         int x=0;        
156         do{
157                 new_option=getopt_long(argc,argv,short_options,long_options,NULL);              
158                 if (new_option=='h'){
159                         printf("help\n");
160                         printf("-h will print the commands available\n");
161                         printf("-c will continue after a test fails else the program will exit\n");
162                         printf("-v will print all messages\n");
163                         printf("-q quiet mode only the number of tests passed and failed will be printed\n");
164                         printf("-t [number] set yaffs_trace to number\n");
165                         printf("-n [number] sets the number of random loops to run after the the test has run\n");
166                         exit(0);
167                 } else if (new_option=='c') {
168                         set_exit_on_error(0);
169                 } else if (new_option=='q') {
170                         set_print_level(-3);
171                 } else if (new_option=='t') {
172                         trace = atoi(optarg);
173                 }  else if (new_option=='v') {
174                         set_print_level(5);
175                 } else if (new_option=='n') {
176                         number_of_random_tests=atoi(optarg);
177                 }
178
179         }while (new_option!=-1);
180         yaffs_start_up();
181         yaffs_set_trace(trace);
182
183 }