summaryrefslogtreecommitdiff
path: root/apps/examples/smart_test/smart_test.c
blob: efce1b0bf2088f5517b0dbf8eb8fd32799c1aa95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/****************************************************************************
 * apps/system/smart_test/smart_test.c
 *
 *   Copyright (C) 2013 Ken Pettit. All rights reserved.
 *   Author: Ken Pettit <pettitkd@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>
#include <nuttx/progmem.h>
#include <sys/stat.h>

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define SMART_TEST_LINE_COUNT 2000
#define SMART_TEST_SEEK_WRITE_COUNT 2000

/****************************************************************************
 * Private data
 ****************************************************************************/

static int g_linePos[SMART_TEST_LINE_COUNT];
static int g_lineLen[SMART_TEST_LINE_COUNT];

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: smart_create_test_file
 *
 * Description: Creates the test file with test data that we will use
 *              to conduct the test.
 *
 ****************************************************************************/

static int smart_create_test_file(char *filename)
{
  FILE*     fd;
  int       x;
  char      string[80];

  /* Try to open the file */

  printf("Creating file %s for write mode\n", filename);

  fd = fopen(filename, "w+");

  if (fd == NULL)
    {
      printf("Unable to create file %s\n", filename);
      return -ENOENT;
    }

  /* Write data to the file.  The data consists of a bunch of
   * lines, each with a line number and the offset within the
   * file where that file starts.
   */

  printf("Writing test data.  %d lines to write\n", SMART_TEST_LINE_COUNT);
  for (x = 0; x < SMART_TEST_LINE_COUNT; x++)
    {
      g_linePos[x] = ftell(fd);

      sprintf(string, "This is line %d at offset %d\n", x, g_linePos[x]);
      g_lineLen[x] = strlen(string);
      fprintf(fd, "%s", string); 

      printf("\r%d", x);
      fflush(stdout);
    } 
  
  /* Close the file */

  printf("\r\nDone.\n");

  fclose(fd);

  return OK;
}

/****************************************************************************
 * Name: smart_seek_test
 *
 * Description: Conducts a seek test on the file.
 *
 ****************************************************************************/

static int smart_seek_test(char *filename)
{
  FILE*     fd;
  int       x;
  int       index;
  char      readstring[80];
  char      cmpstring[80];

  printf("Performing 2000 random seek tests\n");

  fd = fopen(filename, "r");
  if (fd == NULL)
    {
      printf("Unable to open file %s\n", filename);
      return -ENOENT;
    }

  srand(23);
  for (x = 0; x < 2000; x++)
    {
      /* Get random line to seek to */

      index = rand();
      while (index >= SMART_TEST_LINE_COUNT)
        {
          index -= SMART_TEST_LINE_COUNT;
        }

      fseek(fd, g_linePos[index], SEEK_SET);
      fread(readstring, 1, g_lineLen[index], fd);
      readstring[g_lineLen[index]] = '\0';

      sprintf(cmpstring, "This is line %d at offset %d\n",
              index, g_linePos[index]);

      if (strcmp(readstring, cmpstring) != 0)
        {
          printf("\nSeek error on line %d\n", index);
        }

      printf("\r%d", x);
      fflush(stdout);
    }

  fclose(fd);

  return OK;
}

/****************************************************************************
 * Name: smart_append_test
 *
 * Description: Conducts an append test on the file.
 *
 ****************************************************************************/

static int smart_append_test(char *filename)
{
  FILE*     fd;
  int       pos;
  char      readstring[80];

  fd = fopen(filename, "a+");
  if (fd == NULL)
    {
      printf("Unable to open file %s\n", filename);
      return -ENOENT;
    }

  /* Now write some data to the end of the file */

  fprintf(fd, "This is a test of the append.\n");
  pos = ftell(fd);

  /* Now seek to the end of the file and ensure that is where
   * pos is.
   */
  
  fseek(fd, 0, SEEK_END);
  if (ftell(fd) != pos)
    {
      printf("Error opening for append ... data not at EOF\n");
    }

  /* Now seek to that position and read the data back */

  fseek(fd, 30, SEEK_END);
  fread(readstring, 1, 30, fd);
  readstring[30] = '\0';
  if (strcmp(readstring, "This is a test of the append.\n") != 0)
    {
      printf("Append test failed\n");
    }
  else
    {
      printf("Append test passed\n");
    }

  fclose(fd);

  return OK;
}

/****************************************************************************
 * Name: smart_seek_with_write_test
 *
 * Description: Conducts an append test on the file.
 *
 ****************************************************************************/

static int smart_seek_with_write_test(char *filename)
{
  FILE*     fd;
  int       x;
  int       index;
  char      temp;
  char      readstring[80];
  char      cmpstring[80];
  int       c, s1, s2, len;

  printf("Performing %d random seek with write tests\n",
         SMART_TEST_SEEK_WRITE_COUNT);

  fd = fopen(filename, "r+");
  if (fd == NULL)
    {
      printf("Unable to open file %s\n", filename);
      return -ENOENT;
    }

  index = 0;
  for (x = 0; x < SMART_TEST_SEEK_WRITE_COUNT; x++)
    {
#if 0
      /* Get a random value */

      index = rand();
      while (index >= SMART_TEST_LINE_COUNT)
        {
          index -= SMART_TEST_LINE_COUNT;
        }
#endif
      /* Read the data into the buffer */

      fseek(fd, g_linePos[index], SEEK_SET);
      fread(readstring, 1, g_lineLen[index], fd);
      readstring[g_lineLen[index]] = '\0';

      /* Scramble the data in the line */

      len = strlen(readstring);
      for (c = 0; c < 100; c++)
        {
          s1 = rand() % len;
          s2 = rand() % len;

          temp = readstring[s1];
          readstring[s1] = readstring[s2];
          readstring[s2] = temp;
        }

      /* Now write the data back to the file */

      fseek(fd, g_linePos[index], SEEK_SET);
      fwrite(readstring, 1, g_lineLen[index], fd);
      fflush(fd);

      /* Now read the data back and compare it */

      fseek(fd, g_linePos[index], SEEK_SET);
      fread(cmpstring, 1, g_lineLen[index], fd);
      cmpstring[g_lineLen[index]] = '\0';

      if (strcmp(readstring, cmpstring) != 0)
        {
          printf("\nCompare failure on line %d\n", index);
          break;
        }

      printf("\r%d", x);
      fflush(stdout);

      /* On to next line */

      if (++index >= SMART_TEST_LINE_COUNT)
        {
          index = 0;
        }
    }

  printf("\n");

  fclose(fd);

  return OK;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

int smart_test_main(int argc, char *argv[])
{
  int ret;

  /* Argument given? */

  if (argc < 2)
    {
      fprintf(stderr, "usage: smart_test smart_mounted_filename\n");
      return -1;
    }

  /* Create a test file */

  if ((ret = smart_create_test_file(argv[1])) < 0)
    {
      return ret;
    }

  /* Conduct a seek test */

  if ((ret = smart_seek_test(argv[1])) < 0)
    {
      return ret;
    }

  /* Conduct an append test */

  if ((ret = smart_append_test(argv[1])) < 0)
    {
      return ret;
    }

  /* Conduct a seek with write test */

  if ((ret = smart_seek_with_write_test(argv[1])) < 0)
    {
      return ret;
    }

  /* Delete the file */

  return 0;
}