1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
8 * Licensed under the Apache License, Version 2.0 (the License);
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include "libsystem/libsystem.h"
30 #define TEST_READ_WRITE_FILE "/tmp/test-read-write"
31 #define TEST_STRING "L!i@b#s$y%s^t&e*m(T)e-s=tS`t_r+i|n~g"
32 #define TEST_INT32 0x7fabcdef
33 #define TEST_UINT32 0xffabcdef
34 #define TEST_INT64 0x7fabcdef00abcdef
35 #define TEST_UINT64 0xffabcdef00abcdef
37 static void test_string_read_write(void) {
38 _cleanup_free_ char *str = NULL;
40 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
42 assert(write_str_to_path(TEST_READ_WRITE_FILE, TEST_STRING, 0) >= 0);
43 assert(read_one_line_from_path(TEST_READ_WRITE_FILE, &str) >= 0);
45 assert(streq(str, TEST_STRING));
47 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
50 static void test_int32_read_write(void) {
53 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
55 assert(write_int32_to_path(TEST_READ_WRITE_FILE, TEST_INT32, 0) >= 0);
56 assert(read_int32_from_path(TEST_READ_WRITE_FILE, &i) >= 0);
58 assert(i == TEST_INT32);
60 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
63 static void test_uint32_read_write(void) {
66 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
68 assert(write_uint32_to_path(TEST_READ_WRITE_FILE, TEST_UINT32, 0) >= 0);
69 assert(read_uint32_from_path(TEST_READ_WRITE_FILE, &u) >= 0);
71 assert(u == TEST_UINT32);
73 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
76 static void test_int64_read_write(void) {
79 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
81 assert(write_int64_to_path(TEST_READ_WRITE_FILE, TEST_INT64, 0) >= 0);
82 assert(read_int64_from_path(TEST_READ_WRITE_FILE, &i) >= 0);
84 assert(i == TEST_INT64);
86 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
89 static void test_uint64_read_write(void) {
92 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
94 assert(write_uint64_to_path(TEST_READ_WRITE_FILE, TEST_UINT64, 0) >= 0);
95 assert(read_uint64_from_path(TEST_READ_WRITE_FILE, &u) >= 0);
97 assert(u == TEST_UINT64);
99 assert(unlink(TEST_READ_WRITE_FILE) == 0 || errno == ENOENT);
102 int main(int argc, char *argv[]) {
103 test_string_read_write();
104 test_int32_read_write();
105 test_uint32_read_write();
106 test_int64_read_write();
107 test_uint64_read_write();