1 /***************************************************************************
2  *  tests/mng/test_config.cpp
3  *
4  *  Part of FOXXLL. See http://foxxll.org
5  *
6  *  Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
7  *
8  *  Distributed under the Boost Software License, Version 1.0.
9  *  (See accompanying file LICENSE_1_0.txt or copy at
10  *  http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #include <tlx/die.hpp>
14 
15 #include <foxxll/mng.hpp>
16 
test1()17 void test1()
18 {
19     // test disk_config parser:
20 
21     foxxll::disk_config cfg;
22 
23     cfg.parse_line("disk=/var/tmp/foxxll.tmp, 100 GiB , syscall unlink direct=on");
24 
25     die_unequal(cfg.path, "/var/tmp/foxxll.tmp");
26     die_unequal(cfg.size, 100 * 1024 * 1024 * uint64_t(1024));
27     die_unequal(cfg.fileio_string(), "syscall direct=on unlink_on_open");
28 
29     // test disk_config parser:
30 
31     cfg.parse_line("disk=/var/tmp/foxxll.tmp, 100 , wincall queue=5 delete_on_exit direct=on");
32 
33     die_unequal(cfg.path, "/var/tmp/foxxll.tmp");
34     die_unequal(cfg.size, 100 * 1024 * uint64_t(1024));
35     die_unequal(cfg.fileio_string(), "wincall delete_on_exit direct=on queue=5");
36     die_unequal(cfg.queue, 5);
37     die_unequal(cfg.direct, foxxll::disk_config::DIRECT_ON);
38 
39     // bad configurations
40 
41     die_unless_throws(
42         cfg.parse_line("disk=/var/tmp/foxxll.tmp, 100 GiB, wincall_fileperblock unlink direct=on"),
43         std::runtime_error
44     );
45 
46     die_unless_throws(
47         cfg.parse_line("disk=/var/tmp/foxxll.tmp,0x,syscall"),
48         std::runtime_error
49     );
50 }
51 
test2()52 void test2()
53 {
54 #if !FOXXLL_WINDOWS
55     // test user-supplied configuration
56 
57     foxxll::config* config = foxxll::config::get_instance();
58 
59     {
60         foxxll::disk_config disk1("/tmp/foxxll-1.tmp", 100 * 1024 * 1024,
61                                   "syscall");
62         disk1.unlink_on_open = true;
63         disk1.direct = foxxll::disk_config::DIRECT_OFF;
64 
65         die_unequal(disk1.path, "/tmp/foxxll-1.tmp");
66         die_unequal(disk1.size, 100 * 1024 * uint64_t(1024));
67         die_unequal(disk1.autogrow, 1);
68         die_unequal(
69             disk1.fileio_string(),
70             "syscall direct=off unlink_on_open"
71         );
72 
73         config->add_disk(disk1);
74 
75         foxxll::disk_config disk2("/tmp/foxxll-2.tmp", 200 * 1024 * 1024,
76                                   "syscall autogrow=no direct=off");
77         disk2.unlink_on_open = true;
78 
79         die_unequal(disk2.path, "/tmp/foxxll-2.tmp");
80         die_unequal(disk2.size, 200 * 1024 * uint64_t(1024));
81         die_unequal(
82             disk2.fileio_string(),
83             "syscall autogrow=no direct=off unlink_on_open"
84         );
85         die_unequal(disk2.direct, 0);
86 
87         config->add_disk(disk2);
88     }
89 
90     die_unequal(config->disks_number(), 2u);
91     die_unequal(config->total_size(), 300u * 1024 * 1024);
92 
93     // construct block_manager with user-supplied config
94 
95     foxxll::block_manager* bm = foxxll::block_manager::get_instance();
96 
97     die_unequal(bm->total_bytes(), 300u * 1024 * 1024);
98     die_unequal(bm->free_bytes(), 300u * 1024 * 1024);
99 
100 #endif
101 }
102 
main()103 int main()
104 {
105     test1();
106     test2();
107 
108     return 0;
109 }
110 
111 /**************************************************************************/
112