yaffs direst tests: Clean up error to string converter
[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
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         int output=0;
30         char message[30];
31         message[0]='\0';
32         unsigned int x=0;
33
34         init_quick_tests(argc, argv);
35         print_message("\n\nrunning quick tests for yaffs\n\n", 0);
36
37         for (x=0;x<total_number_of_tests;x++){
38         /*      output=yaffs_open(FILE_PATH,O_CREAT | O_RDWR, FILE_MODE);
39                 printf("yaffs_open %d \n",output);
40                 printf("yaffs_close %d \n",yaffs_close(output));
41         */      yaffs_set_error(0);     /*reset the last error to 0 */
42                 sprintf(message,"\nrunning test: %s \n",test_list[x].name_of_test);
43                 print_message(message,3);
44                 output=test_list[x].p_function();       /*run test*/
45                 if (output>=0){
46                         /*test has passed*/
47                         sprintf(message,"\ttest %s passed\n",test_list[x].name_of_test);
48                         print_message(message,3); 
49                         num_of_tests_pass++;
50                 } else {
51                         /*test is assumed to have failed*/
52                         //printf("test failed\n");
53                         sprintf(message,"test: %s failed\n",test_list[x].name_of_test);
54                         print_message(message,1);               
55                         num_of_tests_failed ++; 
56                 
57                         get_error();
58                         print_message("\n\n",1);
59                         if (get_exit_on_error()){       
60                                 quit_quick_tests(1);
61                         }
62
63                 }
64                 output=0;
65                 output=test_list[x].p_function_clean(); /*clean the test*/
66                 if (output <0){
67                         /* if the test failed to clean it's self then */
68                         sprintf(message,"test: %s failed to clean\n",test_list[x].name_of_test);
69                         print_message(message,1);               
70                         num_of_tests_failed ++; 
71                         num_of_tests_pass--;
72                         get_error();
73                         printf("\n\n");
74                         if (get_exit_on_error()){
75                                 quit_quick_tests(1);
76                         }
77                         
78                 } else {
79                         sprintf(message,"\ttest clean: %s passed\n",test_list[x].name_of_test);
80                         print_message(message,3);
81                 }
82         }
83         /*this is where the loop should break to*/
84         quit_quick_tests(0);
85 }
86
87 void quit_quick_tests(int exit_code)
88 {
89         char message[30];
90         message[0]='\0';        
91         if (num_of_tests_pass==total_number_of_tests &&  num_of_tests_failed==0){
92                 printf("\t OK \n");
93         }
94         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);
95         yaffs_unmount(YAFFS_MOUNT_POINT);
96         exit(exit_code);
97 }
98
99 void get_error(void)
100 {
101         int error_code=0;
102         char message[30];
103         message[0]='\0';
104
105         error_code=yaffs_get_error();
106         sprintf(message,"yaffs_error code %d\n",error_code);
107         print_message(message,1);
108         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
109         print_message(message,1);
110 }
111
112 void init_quick_tests(int argc, char *argv[])
113 {
114         int trace=0;
115         int x=0;        
116         for (x = 0; x < argc; x++){
117                 if (0==strcmp(argv[x],"-h")){
118                         printf("help\n");
119                         printf("-h will print the commands available\n");
120                         printf("-c will continue after a test failes else the program will exit\n");
121                         printf("-v will print all messages\n");
122                         printf("-q quiet mode only the number of tests passed and failed will be printed\n");
123                         printf("-t [number] set yaffs_trace to number\n");
124                         exit(0);
125                 } else if (0==strcmp(argv[x],"-c")) {
126                         set_exit_on_error(0);
127                 } else if (0==strcmp(argv[x],"-q")) {
128                         set_print_level(-3);
129                 } else if (0==strcmp(argv[x],"-t")) {
130                         trace = atoi(argv[x+1]);
131                 }  else if (0==strcmp(argv[x],"-v")) {
132                         set_print_level(5);
133                 }
134
135         }
136         yaffs_start_up();
137         yaffs_set_trace(trace);
138
139 }