aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/fastlib/io/bufferedfiletest.cpp
blob: 17f295c04d6f361543d25a377287f09624a87e4f (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
// Copyright Yahoo. 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>
#include <filesystem>

namespace fs = std::filesystem;
namespace {

void remove_testfiles()
{
    fs::remove(fs::path("testfile1"));
    fs::remove(fs::path("testfile2"));
    fs::remove(fs::path("testfile3"));
    fs::remove(fs::path("testfile4"));
    fs::remove(fs::path("testfile5"));
}

}

TEST("main") {
    int value = 0;

    remove_testfiles();

    Fast_BufferedFile bufFile(4096);

    // test 1
    printf ("testing 11 byte long file\n");
    bufFile.WriteOpen("testfile1");
    bufFile.addNum(1,10,' ');
    ASSERT_TRUE(bufFile.CheckedWrite("\n",1));
    ASSERT_TRUE(bufFile.Close());
    ASSERT_EQUAL(11u, fs::file_size(fs::path("testfile1")));
    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);
    ASSERT_TRUE(bufFile.CheckedWrite(buf,4095)); // write almost 4K
    ASSERT_TRUE(bufFile.Close());
    ASSERT_EQUAL(4095u, fs::file_size(fs::path("testfile2")));
    printf (" -- SUCCESS\n\n");

    // test 3
    printf ("testing 4096 byte long file\n");
    bufFile.WriteOpen("testfile3");
    ASSERT_TRUE(bufFile.CheckedWrite(buf,4096));  // write exactly 4K
    ASSERT_TRUE(bufFile.Close());
    ASSERT_EQUAL(4096u, fs::file_size(fs::path("testfile3")));
    printf (" -- SUCCESS\n\n");

    // test 4
    printf ("testing 4097 byte long file\n");
    bufFile.WriteOpen("testfile4");
    ASSERT_TRUE(bufFile.CheckedWrite(buf,4097));   // write a bit over 4K
    ASSERT_TRUE(bufFile.Close());
    ASSERT_EQUAL(4097u, fs::file_size(fs::path("testfile4")));
    printf (" -- SUCCESS\n\n");

    // test 5
    printf ("testing 610000 byte long file with repeated addNum\n");
    bufFile.WriteOpen("testfile5");
    for (int i = 0; i < 10000; i++) {
        for (int j = 0; j < 10; j++) {
            bufFile.addNum(value,6,' ');
            value++;
        }
        ASSERT_TRUE(bufFile.CheckedWrite("\n",1));
    }
    ASSERT_TRUE(bufFile.Close());
    ASSERT_EQUAL(610000u, fs::file_size(fs::path("testfile5")));
    printf (" -- SUCCESS\n\n");

    remove_testfiles();

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

TEST_MAIN() { TEST_RUN_ALL(); }