Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-file.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2011-2012 Brazil
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <gcutter.h>
20 #include <cppcutter.h>
21 
22 #include <grn-assertions.h>
23 #include <dat/file.hpp>
24 
25 #include <cstring>
26 
27 namespace test_dat_file
28 {
29  const gchar *base_dir = NULL;
30 
31  void cut_setup(void)
32  {
34  cut_remove_path(base_dir, NULL);
35  g_mkdir_with_parents(base_dir, 0755);
36  }
37 
38  void cut_teardown(void)
39  {
40  if (base_dir) {
41  cut_remove_path(base_dir, NULL);
42  }
43  }
44 
45  void test_invalid_file(void)
46  {
47  const grn::dat::File file;
48 
49  cppcut_assert_null(file.ptr());
50  cppcut_assert_equal(grn::dat::UInt64(0), file.size());
51  }
52 
54  {
55  grn::dat::File file;
56 
57  try {
58  file.create(NULL, 0);
59  cut_fail("A zero-byte request is not allowed.");
60  } catch (const grn::dat::Exception &) {
61  }
62 
63  file.create(NULL, 32);
64  cppcut_assert_not_null(file.ptr());
65  cppcut_assert_equal(grn::dat::UInt64(32), file.size());
66 
67  grn::dat::UInt8 * const buf = static_cast<grn::dat::UInt8 *>(file.ptr());
68  for (grn::dat::UInt64 i = 0; i < file.size(); ++i) {
69  buf[i] = static_cast<grn::dat::UInt8>(i);
70  cppcut_assert_equal(static_cast<grn::dat::UInt8>(i), buf[i]);
71  }
72  }
73 
75  {
76  char path[PATH_MAX];
77  std::strcpy(path, base_dir);
78  std::strcat(path, "/test_create_with_path.dat");
79 
80  grn::dat::File file;
81 
82  try {
83  file.create(path, 0);
84  cut_fail("A zero-byte request is not allowed.");
85  } catch (const grn::dat::Exception &) {
86  }
87 
88  file.create(path, 32);
89  cppcut_assert_not_null(file.ptr());
90  cppcut_assert_equal(grn::dat::UInt64(32), file.size());
91 
92  grn::dat::UInt8 * const buf = static_cast<grn::dat::UInt8 *>(file.ptr());
93  for (grn::dat::UInt64 i = 0; i < file.size(); ++i) {
94  buf[i] = static_cast<grn::dat::UInt8>(i);
95  cppcut_assert_equal(static_cast<grn::dat::UInt8>(i), buf[i]);
96  }
97  }
98 
99  void test_open(void)
100  {
101  char path[PATH_MAX];
102  std::strcpy(path, base_dir);
103  std::strcat(path, "/test_open.dat");
104 
105  grn::dat::File file;
106 
107  try {
108  file.open(NULL);
109  cut_fail("A null-path request is not allowed.");
110  } catch (const grn::dat::Exception &) {
111  }
112 
113  file.create(path, 32);
114  std::strcpy(static_cast<char *>(file.ptr()), "This is a pen.");
115 
116  file.close();
117  cut_assert_null(file.ptr());
118  cppcut_assert_equal(grn::dat::UInt64(0), file.size());
119 
120  file.open(path);
121  cppcut_assert_not_null(file.ptr());
122  cppcut_assert_equal(grn::dat::UInt64(32), file.size());
123  cppcut_assert_equal(0, std::strcmp(static_cast<char *>(file.ptr()),
124  "This is a pen."));
125  }
126 
127  void test_swap(void)
128  {
129  grn::dat::File file;
130 
131  file.create(NULL, 100);
132  cppcut_assert_not_null(file.ptr());
133  cppcut_assert_equal(grn::dat::UInt64(100), file.size());
134 
135  grn::dat::File file_new;
136  file_new.swap(&file);
137 
138  cut_assert_null(file.ptr());
139  cppcut_assert_equal(grn::dat::UInt64(0), file.size());
140 
141  cppcut_assert_not_null(file_new.ptr());
142  cppcut_assert_equal(grn::dat::UInt64(100), file_new.size());
143  }
144 }