summaryrefslogtreecommitdiffstats
path: root/fastlib/src/vespa/fastlib/io/tests/bufferedfiletest.cpp
blob: 1a60a80dd7cca70562ba93c015f11603476f75f4 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/vespalib/testkit/test_kit.h>


TEST("main") {
    int i = 0;
    int j = 0;
    int value = 0;
    FastOS_StatInfo statInfo;

    FastOS_File::Delete("testfile1");
    FastOS_File::Delete("testfile2");
    FastOS_File::Delete("testfile3");
    FastOS_File::Delete("testfile4");
    FastOS_File::Delete("testfile5");

    Fast_BufferedFile bufFile(4096);

    // test 1
    printf ("testing 11 byte long file\n");
    bufFile.WriteOpen("testfile1");
    bufFile.addNum(1,10,' ');
    bufFile.CheckedWrite("\n",1);
    bufFile.Close();
    FastOS_File::Stat("testfile1", &statInfo);
    if (statInfo._size != 11) {
        printf (" -- FAILURE\n\n");
        TEST_FATAL("exit 1");
    }
    printf (" -- SUCCESS\n\n");

    // test 2
    printf ("testing 4095 byte long file\n");
    bufFile.WriteOpen("testfile2");
    char buf[8192]; // allocate 8K buffer
    memset(buf,0xff,8192);
    bufFile.CheckedWrite(buf,4095); // write almost 4K
    bufFile.Close();
    FastOS_File::Stat("testfile2", &statInfo);
    if (statInfo._size != 4095) {
        printf (" -- FAILURE\n\n");
        TEST_FATAL("exit 1");
    }
    printf (" -- SUCCESS\n\n");

    // test 3
    printf ("testing 4096 byte long file\n");
    bufFile.WriteOpen("testfile3");
    bufFile.CheckedWrite(buf,4096);  // write exactly 4K
    bufFile.Close();
    FastOS_File::Stat("testfile3", &statInfo);
    if (statInfo._size != 4096) {
        printf (" -- FAILURE\n\n");
        TEST_FATAL("exit 1");
    }
    printf (" -- SUCCESS\n\n");

    // test 4
    printf ("testing 4097 byte long file\n");
    bufFile.WriteOpen("testfile4");
    bufFile.CheckedWrite(buf,4097);   // write a bit over 4K
    bufFile.Close();
    FastOS_File::Stat("testfile4", &statInfo);
    if (statInfo._size != 4097) {
        printf (" -- FAILURE\n\n");
        TEST_FATAL("exit 1");
    }
    printf (" -- SUCCESS\n\n");

    // test 5
    printf ("testing 610000 byte long file with repeated addNum\n");
    bufFile.WriteOpen("testfile5");
    for (i = 0; i < 10000; i++) {
        for (j = 0; j < 10; j++) {
            bufFile.addNum(value,6,' ');
            value++;
        }
        bufFile.CheckedWrite("\n",1);
    }
    bufFile.Close();
    FastOS_File::Stat("testfile5", &statInfo);
    if (statInfo._size != 610000) {
        printf (" -- FAILURE\n\n");
        TEST_FATAL("exit 1");
    }
    printf (" -- SUCCESS\n\n");

    FastOS_File::Delete("testfile1");
    FastOS_File::Delete("testfile2");
    FastOS_File::Delete("testfile3");
    FastOS_File::Delete("testfile4");
    FastOS_File::Delete("testfile5");

    printf ("All tests OK for bufferedfiletest\n");
    printf (" -- SUCCESS\n\n");
}

TEST_MAIN() { TEST_RUN_ALL(); }