File: test_linebuf.cc

package info (click to toggle)
cssc 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 11,368 kB
  • sloc: cpp: 39,446; ansic: 17,403; sh: 11,328; python: 3,923; makefile: 1,929; perl: 342; awk: 15; sed: 15
file content (180 lines) | stat: -rw-r--r-- 5,027 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * test_linebuf.cc: Part of GNU CSSC.
 *
 * Copyright (C) 2011, 2014, 2019 Free Software Foundation, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Unit tests for cssc_linebuf.
 *
 */
#include <config.h>
#include <stdio.h>
#include "linebuf.h"
#include <gtest/gtest.h>

class LineBufTest : public ::testing::Test {
public:
  cssc_linebuf three_colon;
  cssc_linebuf two_slash_with_null;

  static FILE * MakeFile(const char *data, size_t len)
  {
    FILE *fp;

    fp = tmpfile();
    fwrite (data, 1, len, fp);
    rewind (fp);
    return fp;
  }
  
  virtual void SetUp() 
  {
    FILE *fp;

    fp = MakeFile("hello:there:world", 17);
    ASSERT_EQ(0, three_colon.read_line(fp));
    fclose (fp);

    fp = MakeFile ("one\0two/three\n", 14);
    ASSERT_EQ(0, two_slash_with_null.read_line(fp));
    fclose (fp);
  }
};

TEST_F(LineBufTest, InitialState) {
  cssc_linebuf empty;
  const char *p = empty.c_str();
  EXPECT_NE((const char*)0, p);
  /* content is not guaranteed at all. */
}

TEST_F(LineBufTest, ArrayAccess) {
  EXPECT_EQ('h', three_colon[0]);
  EXPECT_EQ('e', three_colon[1]);
  EXPECT_EQ('\0', three_colon[17]);
}

TEST_F(LineBufTest, C_Str) {
  const char *p = three_colon.c_str();
  EXPECT_EQ('h', p[0]);
  EXPECT_EQ('e', p[1]);
  EXPECT_EQ('\0', p[17]);

  const cssc_linebuf& ref(three_colon);
  const char *cp = ref.c_str();
  EXPECT_EQ('h', cp[0]);
  EXPECT_EQ('e', cp[1]);
  EXPECT_EQ('\0', cp[17]);
}

TEST_F(LineBufTest, SetChar) {
  EXPECT_EQ('h', three_colon[0]);
  EXPECT_EQ('e', three_colon[1]);
  three_colon.set_char(0, 'y');
  EXPECT_EQ('y', three_colon[0]);
  EXPECT_EQ('e', three_colon[1]);
}

TEST_F(LineBufTest, SplitBasic) {
  int num_fields;
  char *fields[5];
  char dummy;
  fields[4] = 0;
  fields[3] = &dummy;
  num_fields = three_colon.split(0, fields, 4, ':');
  ASSERT_EQ(3, num_fields);
  EXPECT_EQ(0, strcmp("hello", fields[0])) << "fields[0] is" << fields[0];
  EXPECT_EQ(0, strcmp("there", fields[1])) << "fields[1] is" << fields[1];
  EXPECT_EQ(0, strcmp("world", fields[2])) << "fields[2] is" << fields[2];
  /* Check that fields[] is not assigned to beyond the specified limit. */
  EXPECT_EQ(&dummy, fields[3]);
}

TEST_F(LineBufTest, SplitMissing) {
  int num_fields;
  char *fields[2];
  /* Same again, using a delimiter not in the string. */
  num_fields = three_colon.split(0, fields, 2, '/');
  ASSERT_EQ(1, num_fields);
  EXPECT_EQ(0, strcmp("hello:there:world", fields[0]));
}

TEST_F(LineBufTest, SplitOffset) {
  int num_fields;
  char *fields[3];
  /* Same again, using a delimiter not in the string. */
  num_fields = three_colon.split(3, fields, 3, ':');
  ASSERT_EQ(3, num_fields);
  EXPECT_EQ(0, strcmp("lo",    fields[0])) << "fields[0] is" << fields[0];
  EXPECT_EQ(0, strcmp("there", fields[1])) << "fields[1] is" << fields[1];
  EXPECT_EQ(0, strcmp("world", fields[2])) << "fields[2] is" << fields[2];
}

TEST_F(LineBufTest, SplitLimit) {
  int num_fields;
  char *fields[2];
  /* Same again, using a delimiter not in the string. */
  num_fields = three_colon.split(0, fields, 1, ':');
  ASSERT_EQ(1, num_fields);
  EXPECT_EQ(0, strcmp("hello", fields[0])) << "fields[0] is" << fields[0];
}

TEST_F(LineBufTest, Keywords) {
  EXPECT_EQ(0, three_colon.check_id_keywords());
  EXPECT_EQ(0, two_slash_with_null.check_id_keywords());
  cssc_linebuf kwbuf;
  FILE *fp = MakeFile ("hello %M% world\n", 16);
  ASSERT_EQ(0, kwbuf.read_line(fp));
  fclose (fp);
  EXPECT_EQ(1, kwbuf.check_id_keywords());
}

TEST_F(LineBufTest, Write) {
  FILE *fp = tmpfile();
  three_colon.write(fp);
  three_colon.set_char(0, 't');
  rewind (fp);
  three_colon.write(fp);
  rewind (fp);
  /* Now read back the modified data. */
  cssc_linebuf buf;
  ASSERT_EQ(0, buf.read_line(fp));
  EXPECT_EQ('t', buf[0]);
  EXPECT_EQ('e', buf[1]);
  fclose (fp);
}

TEST_F(LineBufTest, ReadLineSequence) {
  FILE *fp = tmpfile();
  fprintf (fp, "one\ntwo\nthree\n");
  rewind (fp);
  cssc_linebuf b;
  
  ASSERT_EQ(0, b.read_line(fp));
  EXPECT_EQ(0, strcmp(b.c_str(), "one\n"))
    << "actual value was '" << b.c_str() << "'";
  
  ASSERT_EQ(0, b.read_line(fp));
  EXPECT_EQ(0, strcmp(b.c_str(), "two\n"))
    << "actual value was '" << b.c_str() << "'";

  ASSERT_EQ(0, b.read_line(fp));
  EXPECT_EQ(0, strcmp(b.c_str(), "three\n"))
    << "actual value was '" << b.c_str() << "'";

  /* Make sure we also detect EOF. */
  ASSERT_EQ(1, b.read_line(fp));
  fclose (fp);
}