Fix unmatched temporary buffer allocations
[yaffs2.git] / direct / test-framework / unit_tests / quick_tests / quick_tests.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Timothy Manning <timothy@yaffs.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include "quick_tests.h"
14
15
16 int random_seed;
17 int simulate_power_failure = 0;
18
19
20
21
22 static int number_of_random_tests=0;
23 static unsigned int num_of_tests_pass=0;
24 static unsigned int num_of_tests_failed=0;
25 static unsigned int total_number_of_tests=(sizeof(test_list)/sizeof(test_template));
26
27
28 const struct option long_options[]={
29         {"help",        0,NULL,'h'},
30         {"quiet",       0,NULL,'q'},
31         {"number",      1,NULL,'n'},
32         {"trace",       1,NULL,'t'},
33         {"continue",    0,NULL,'c'},
34         {"verbose",     0,NULL,'v'}
35 };
36
37 const char short_options[]="hqn:t:cv";
38
39
40
41 int main(int argc, char *argv[])
42 {
43         int x=0;
44
45         init_quick_tests(argc, argv);
46         logical_run_of_tests();
47         for (x=0;x<number_of_random_tests;x ++){
48                 run_random_test_loop();
49         }       
50         /*this is where the loop should break to*/
51         quit_quick_tests();
52         
53 }
54
55
56 void run_random_test_loop(void)
57 {
58         int id=0;
59         unsigned 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         int y= 0;
81         char message[200];
82         message[0]='\0';
83
84         yaffs_mkdir(TEST_DIR,S_IWRITE | S_IREAD);
85         yaffs_set_error(0);     /*reset the last error to 0 */
86         //printf("foo exists %d\n",test_yaffs_open());
87         sprintf(message,"\nrunning test: %s \n",test_list[x].name_of_test);
88         print_message(message,3);
89         output=test_list[x].p_function();       /*run test*/
90         if (output>=0){
91                 /*test has passed*/
92                 sprintf(message,"\ttest %s passed\n",test_list[x].name_of_test);
93                 print_message(message,3); 
94                 num_of_tests_pass++;
95         } else {
96                 /*test is assumed to have failed*/
97                 printf("test failed\n");
98                 sprintf(message,"test: %s failed\n",test_list[x].name_of_test);
99                 print_message(message,1);               
100                 num_of_tests_failed ++; 
101         
102                 get_error();
103                 print_message("\n\n",1);
104                 if (get_exit_on_error()){       
105                         quit_quick_tests();
106                 }
107         }
108         output=0;
109         output=test_list[x].p_function_clean(); /*clean the test*/
110         if (output <0){
111                 /* if the test failed to clean it's self then */
112                 sprintf(message,"test: %s failed to clean\n",test_list[x].name_of_test);
113                 print_message(message,1);               
114                 num_of_tests_failed ++; 
115                 num_of_tests_pass--;
116                 get_error();
117                 printf("\n\n");
118                 if (get_exit_on_error()){
119                         quit_quick_tests();
120                 }
121                 
122         } else {
123                 sprintf(message,"\ttest clean: %s passed\n",test_list[x].name_of_test);
124                 print_message(message,3);
125         }
126         /* close all open handles */
127         for (y=0; y<100;y++){
128                 yaffs_close(y);
129         }
130         delete_dir(TEST_DIR);
131 }
132
133 void quit_quick_tests()
134 {
135         /*char message[30];
136         message[0]='\0';
137         */      
138         if (num_of_tests_pass==total_number_of_tests &&  num_of_tests_failed==0){
139                 printf("\t OK --all tests passed\n");
140         }
141         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);
142         yaffs_unmount(YAFFS_MOUNT_POINT);
143
144         //remember that the exit_code needs to be 0 when all the tests pass.
145         int exit_code = num_of_tests_failed != 0;
146         exit(exit_code);
147 }
148
149
150 void init_quick_tests(int argc, char *argv[])
151 {
152         int trace=0;
153         int new_option;
154         //int x=0;      
155         do{
156                 new_option=getopt_long(argc,argv,short_options,long_options,NULL);              
157                 if (new_option=='h'){
158                         printf("help\n");
159                         printf("-h will print the commands available\n");
160                         printf("-c will continue after a test fails else the program will exit\n");
161                         printf("-v will print all messages\n");
162                         printf("-q quiet mode only the number of tests passed and failed will be printed\n");
163                         printf("-t [number] set yaffs_trace to number\n");
164                         printf("-n [number] sets the number of random loops to run after the the test has run\n");
165                         exit(0);
166                 } else if (new_option=='c') {
167                         set_exit_on_error(0);
168                 } else if (new_option=='q') {
169                         set_print_level(-3);
170                 } else if (new_option=='t') {
171                         trace = atoi(optarg);
172                 }  else if (new_option=='v') {
173                         set_print_level(5);
174                 } else if (new_option=='n') {
175                         number_of_random_tests=atoi(optarg);
176                 }
177
178         }while (new_option!=-1);
179         yaffs_start_up();
180         yaffs_set_trace(trace);
181
182 }