1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : #include <stdio.h>
8 :
9 : #include "native_client/src/include/portability.h"
10 : #include "native_client/src/shared/gio/gio.h"
11 : #include "native_client/src/shared/gio/gio_test_base.h"
12 : #include "gtest/gtest.h"
13 :
14 : namespace {
15 :
16 : // Premade files set from argv. These are only read (not written to).
17 : // Must be a file with contents ['A'+0, 'A'+1, ..., 'A'+31]
18 : char* g_premade_textfile = NULL;
19 : // Must be a file with contents [0, 1, 2, ..., 31]
20 : char* g_premade_binfile = NULL;
21 : char* g_temp_file = NULL;
22 :
23 : /** Parse arguments for input files and checks that there are enough args. */
24 1 : void parse_args(int argc, char *argv[]) {
25 : int opt;
26 1 : while (-1 != (opt = getopt(argc, argv, "t:x:b:"))) {
27 1 : switch (opt) {
28 : default:
29 0 : printf("Unknown commandline option %c", opt);
30 0 : FAIL();
31 : case 'x':
32 1 : g_premade_textfile = optarg;
33 1 : break;
34 : case 'b':
35 1 : g_premade_binfile = optarg;
36 1 : break;
37 : case 't':
38 1 : g_temp_file = optarg;
39 : break;
40 : }
41 1 : }
42 :
43 1 : printf("Testing with textfile %s\n", g_premade_textfile);
44 1 : printf("Testing with binfile %s\n", g_premade_binfile);
45 1 : printf("Testing with temp file %s\n", g_temp_file);
46 :
47 1 : ASSERT_NE(reinterpret_cast<char*>(NULL), g_temp_file);
48 1 : ASSERT_NE(reinterpret_cast<char*>(NULL), g_premade_textfile);
49 1 : ASSERT_NE(reinterpret_cast<char*>(NULL), g_premade_binfile);
50 1 : }
51 :
52 :
53 3 : TEST(GioTest, ReadTest) {
54 : struct GioFile my_file;
55 : int ret_code;
56 :
57 1 : ret_code = GioFileCtor(&my_file, g_premade_textfile, "r");
58 1 : EXPECT_EQ(1, ret_code);
59 1 : GioReadTestWithOffset(&my_file.base, 'A');
60 1 : GioCloseTest(&my_file.base);
61 :
62 1 : ret_code = GioFileCtor(&my_file, g_premade_binfile, "rb");
63 1 : EXPECT_EQ(1, ret_code);
64 1 : GioReadTestWithOffset(&my_file.base, 0);
65 1 : GioCloseTest(&my_file.base);
66 1 : }
67 :
68 :
69 1 : void MakeTempFile(struct GioFile* my_file) {
70 1 : int ret_code = GioFileCtor(my_file, g_temp_file, "w+b");
71 1 : EXPECT_EQ(1, ret_code);
72 1 : }
73 :
74 1 : void CloseAndCleanTempFile(struct GioFile* gf) {
75 1 : GioCloseTest(&gf->base);
76 :
77 : // Since we pass in the name of the temp file from Scons, and Scons does not
78 : // know when it is opened/closed to delete the temp file. Delete it here.
79 1 : ASSERT_EQ(0, remove(g_temp_file));
80 1 : }
81 :
82 3 : TEST(GioTest, WriteTest) {
83 : struct GioFile my_file;
84 :
85 1 : MakeTempFile(&my_file);
86 1 : GioWriteTest(&my_file.base, false);
87 1 : CloseAndCleanTempFile(&my_file);
88 1 : }
89 :
90 3 : TEST(GioTest, SeekTest) {
91 : struct GioFile my_file;
92 : int ret_code;
93 :
94 1 : ret_code = GioFileCtor(&my_file, g_premade_textfile, "r");
95 1 : EXPECT_EQ(1, ret_code);
96 1 : GioSeekTestWithOffset(&my_file.base, 'A', false);
97 1 : GioCloseTest(&my_file.base);
98 :
99 1 : ret_code = GioFileCtor(&my_file, g_premade_binfile, "rb");
100 1 : EXPECT_EQ(1, ret_code);
101 1 : GioSeekTestWithOffset(&my_file.base, 0, false);
102 1 : GioCloseTest(&my_file.base);
103 1 : }
104 :
105 : } // namespace
106 :
107 1 : int main(int argc, char* argv[]) {
108 1 : testing::InitGoogleTest(&argc, argv);
109 1 : parse_args(argc, argv);
110 1 : return RUN_ALL_TESTS();
111 1 : }
|