yaffs Finshed direct/timothy_tests/linux_test.
[yaffs2.git] / direct / timothy_tests / linux_tests / lib.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 "lib.h"
15 static char message[200];
16 static int  PRINT_LEVEL = 3;
17 static int exit_on_error_val =1;
18
19 int get_exit_on_error(void)
20 {
21         return exit_on_error_val;
22 }
23
24 void set_exit_on_error(int val)
25 {
26         exit_on_error_val=val;
27 }
28
29 node * linked_list_add_node(int pos,node *head_node)
30 {
31         node *new_node=NULL;
32         if (pos==HEAD){
33                 new_node=malloc(sizeof(node));
34                 memset(new_node, NULL, sizeof(node));
35                 new_node->string=NULL;
36                 new_node->next=head_node;
37                 return new_node;
38         }
39         return NULL;
40 }
41
42 void node_print_pointers(node *current_node)
43 {
44         while (current_node != NULL){
45                 sprintf(message,"current_node: %p, string: %s next_node: %p\n",current_node,current_node->string,current_node->next);
46                 print_message(3,message);
47                 current_node=current_node->next;
48         }
49 }
50
51 int delete_linked_list(node *head_node)
52 {
53         node *next_node=NULL;
54         node *current_node=head_node;
55
56                 while (current_node != NULL){
57                         next_node=current_node->next;
58                         free(current_node);
59                         current_node=next_node;
60                 }
61
62         return 1;
63 }
64
65 char * generate_random_string(unsigned int length)
66 {
67         char string[length+1];
68         unsigned int x;
69         for (x=0;x<(length-1);x++)
70         {
71                 string[x]=(rand() % NAME_RANGE)+65;
72         }
73         string[x]='\0';
74         return string;
75 }
76 void set_print_level(int new_level)
77 {
78         PRINT_LEVEL=new_level;
79 }
80 int get_print_level(void)
81 {
82         return PRINT_LEVEL;
83 }
84 void print_message(char print_level,char *message)
85 {
86         if (print_level <= PRINT_LEVEL){
87                 printf(message);
88         }
89 }
90 unsigned int random_int(void)
91 {
92         return (random()%4294967295);
93 }
94
95 void check_function(int output)
96 {
97         if (output>=0){
98                 print_message(3,"test_passed\n");
99         } else {
100                 print_message(3,"test_failed\n");
101                 get_error_linux();
102         }
103 }
104
105 void get_error_linux(void)
106 {
107         int error_code=0;
108         char message[30];
109         message[0]='\0';
110
111         error_code=errno;
112         sprintf(message,"linux_error code %d\n",error_code);
113         print_message(1,message);
114         
115         strcpy(message,"error is");
116         perror(message);
117 //      sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
118         //perror(message);      
119         //print_message(1,message);
120 }