Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[yaffs2.git] / direct / timothy_tests / handle_tests / handle_test.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 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 "handle_test.h"
15
16 int random_seed;
17 int simulate_power_failure = 0;
18
19 int main()
20 {
21         
22         int test_batch = 1000000;
23         int output =0;
24         unsigned int total_number_of_tests_run=0;
25         yaffs_start_up();
26         yaffs_mount("yaffs2");
27
28         printf("running tests\n");
29
30         output=dup_test();
31         printf("dup test: %d\n ",output);
32
33         while (1){
34                 output = open_close_handle_test(test_batch);
35                 if (output>=0){
36                         total_number_of_tests_run ++;
37                 } else {
38                         get_error();
39                         return -1;
40                 }
41                 printf("total number of tests = %d\n",(total_number_of_tests_run*test_batch));
42         }
43
44
45         return 0;
46 }
47 int dup_test(void){
48         int handle = -1;
49         int handle2 = -1;
50         int output =-1;
51
52         handle =open_handle();
53         if (handle<0){
54                 printf("error: failed to open handle\n");
55                 return -1;
56         }
57         handle2=yaffs_dup(handle);
58         if (handle2<0){
59                 printf("error: failed to open handle2\n");
60                 return -1;
61         }
62         
63         output=yaffs_lseek(handle,20,SEEK_SET);
64         if (output >= 0) {
65                 output = yaffs_lseek(handle,0,SEEK_CUR);
66                 if (output = 20){
67                         printf("dup is working\n");
68                         return 1;
69                 } else if (output <0){
70                         printf("failed to lseek the second time\n");
71                         return -1;
72                 } else {
73                         printf("lseek position is diffrent than expected\n");
74                         return -1;
75                 }
76         } else {
77                 printf("failed to lseek the first time\n");
78                 return -1;
79         }
80
81 }
82
83 int open_close_handle_test(int num_of_tests)
84 {
85         int handle_number=open_handle();
86         int handle=0;
87         int x =0;
88         int output=0;
89         yaffs_close(handle_number);
90         for (x=0;x<num_of_tests;x++){
91                 handle=open_handle();
92                 if (handle != handle_number){
93                         printf("handle number changed\n");
94                         printf("test number= %d\n",x);
95                         printf("expected number = %d\n",handle_number);
96                         printf("handle_number = %d\n",handle);
97                         return -1;
98                 }
99                 output=yaffs_close(handle);
100                 if (output <0){
101                         printf("failed to close file\n");
102                         return -1;
103                 }
104         }
105         return 1;
106 }
107
108 void get_error(void)
109 {
110         int error_code=0;
111         error_code=yaffs_get_error();
112         printf("yaffs_error code %d\n",error_code);
113         printf("error is : %s\n",yaffs_error_to_str(error_code));
114 }
115
116 int open_handle(void)
117 {
118         return yaffs_open(FILE_PATH,O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
119 }
120