"Fossies" - the Fresh Open Source Software Archive

Member "CSSC-1.4.1/unit-tests/test_linebuf.cc" (7 May 2019, 5027 Bytes) of package /linux/privat/CSSC-1.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "test_linebuf.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.4.0_vs_1.4.1.

    1 /*
    2  * test_linebuf.cc: Part of GNU CSSC.
    3  *
    4  * Copyright (C) 2011, 2014, 2019 Free Software Foundation, Inc.
    5  *
    6  * This program is free software: you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation, either version 3 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18  *
   19  * Unit tests for cssc_linebuf.
   20  *
   21  */
   22 #include <config.h>
   23 #include <stdio.h>
   24 #include "linebuf.h"
   25 #include <gtest/gtest.h>
   26 
   27 class LineBufTest : public ::testing::Test {
   28 public:
   29   cssc_linebuf three_colon;
   30   cssc_linebuf two_slash_with_null;
   31 
   32   static FILE * MakeFile(const char *data, size_t len)
   33   {
   34     FILE *fp;
   35 
   36     fp = tmpfile();
   37     fwrite (data, 1, len, fp);
   38     rewind (fp);
   39     return fp;
   40   }
   41   
   42   virtual void SetUp() 
   43   {
   44     FILE *fp;
   45 
   46     fp = MakeFile("hello:there:world", 17);
   47     ASSERT_EQ(0, three_colon.read_line(fp));
   48     fclose (fp);
   49 
   50     fp = MakeFile ("one\0two/three\n", 14);
   51     ASSERT_EQ(0, two_slash_with_null.read_line(fp));
   52     fclose (fp);
   53   }
   54 };
   55 
   56 TEST_F(LineBufTest, InitialState) {
   57   cssc_linebuf empty;
   58   const char *p = empty.c_str();
   59   EXPECT_NE((const char*)0, p);
   60   /* content is not guaranteed at all. */
   61 }
   62 
   63 TEST_F(LineBufTest, ArrayAccess) {
   64   EXPECT_EQ('h', three_colon[0]);
   65   EXPECT_EQ('e', three_colon[1]);
   66   EXPECT_EQ('\0', three_colon[17]);
   67 }
   68 
   69 TEST_F(LineBufTest, C_Str) {
   70   const char *p = three_colon.c_str();
   71   EXPECT_EQ('h', p[0]);
   72   EXPECT_EQ('e', p[1]);
   73   EXPECT_EQ('\0', p[17]);
   74 
   75   const cssc_linebuf& ref(three_colon);
   76   const char *cp = ref.c_str();
   77   EXPECT_EQ('h', cp[0]);
   78   EXPECT_EQ('e', cp[1]);
   79   EXPECT_EQ('\0', cp[17]);
   80 }
   81 
   82 TEST_F(LineBufTest, SetChar) {
   83   EXPECT_EQ('h', three_colon[0]);
   84   EXPECT_EQ('e', three_colon[1]);
   85   three_colon.set_char(0, 'y');
   86   EXPECT_EQ('y', three_colon[0]);
   87   EXPECT_EQ('e', three_colon[1]);
   88 }
   89 
   90 TEST_F(LineBufTest, SplitBasic) {
   91   int num_fields;
   92   char *fields[5];
   93   char dummy;
   94   fields[4] = 0;
   95   fields[3] = &dummy;
   96   num_fields = three_colon.split(0, fields, 4, ':');
   97   ASSERT_EQ(3, num_fields);
   98   EXPECT_EQ(0, strcmp("hello", fields[0])) << "fields[0] is" << fields[0];
   99   EXPECT_EQ(0, strcmp("there", fields[1])) << "fields[1] is" << fields[1];
  100   EXPECT_EQ(0, strcmp("world", fields[2])) << "fields[2] is" << fields[2];
  101   /* Check that fields[] is not assigned to beyond the specified limit. */
  102   EXPECT_EQ(&dummy, fields[3]);
  103 }
  104 
  105 TEST_F(LineBufTest, SplitMissing) {
  106   int num_fields;
  107   char *fields[2];
  108   /* Same again, using a delimiter not in the string. */
  109   num_fields = three_colon.split(0, fields, 2, '/');
  110   ASSERT_EQ(1, num_fields);
  111   EXPECT_EQ(0, strcmp("hello:there:world", fields[0]));
  112 }
  113 
  114 TEST_F(LineBufTest, SplitOffset) {
  115   int num_fields;
  116   char *fields[3];
  117   /* Same again, using a delimiter not in the string. */
  118   num_fields = three_colon.split(3, fields, 3, ':');
  119   ASSERT_EQ(3, num_fields);
  120   EXPECT_EQ(0, strcmp("lo",    fields[0])) << "fields[0] is" << fields[0];
  121   EXPECT_EQ(0, strcmp("there", fields[1])) << "fields[1] is" << fields[1];
  122   EXPECT_EQ(0, strcmp("world", fields[2])) << "fields[2] is" << fields[2];
  123 }
  124 
  125 TEST_F(LineBufTest, SplitLimit) {
  126   int num_fields;
  127   char *fields[2];
  128   /* Same again, using a delimiter not in the string. */
  129   num_fields = three_colon.split(0, fields, 1, ':');
  130   ASSERT_EQ(1, num_fields);
  131   EXPECT_EQ(0, strcmp("hello", fields[0])) << "fields[0] is" << fields[0];
  132 }
  133 
  134 TEST_F(LineBufTest, Keywords) {
  135   EXPECT_EQ(0, three_colon.check_id_keywords());
  136   EXPECT_EQ(0, two_slash_with_null.check_id_keywords());
  137   cssc_linebuf kwbuf;
  138   FILE *fp = MakeFile ("hello %M% world\n", 16);
  139   ASSERT_EQ(0, kwbuf.read_line(fp));
  140   fclose (fp);
  141   EXPECT_EQ(1, kwbuf.check_id_keywords());
  142 }
  143 
  144 TEST_F(LineBufTest, Write) {
  145   FILE *fp = tmpfile();
  146   three_colon.write(fp);
  147   three_colon.set_char(0, 't');
  148   rewind (fp);
  149   three_colon.write(fp);
  150   rewind (fp);
  151   /* Now read back the modified data. */
  152   cssc_linebuf buf;
  153   ASSERT_EQ(0, buf.read_line(fp));
  154   EXPECT_EQ('t', buf[0]);
  155   EXPECT_EQ('e', buf[1]);
  156   fclose (fp);
  157 }
  158 
  159 TEST_F(LineBufTest, ReadLineSequence) {
  160   FILE *fp = tmpfile();
  161   fprintf (fp, "one\ntwo\nthree\n");
  162   rewind (fp);
  163   cssc_linebuf b;
  164   
  165   ASSERT_EQ(0, b.read_line(fp));
  166   EXPECT_EQ(0, strcmp(b.c_str(), "one\n"))
  167     << "actual value was '" << b.c_str() << "'";
  168   
  169   ASSERT_EQ(0, b.read_line(fp));
  170   EXPECT_EQ(0, strcmp(b.c_str(), "two\n"))
  171     << "actual value was '" << b.c_str() << "'";
  172 
  173   ASSERT_EQ(0, b.read_line(fp));
  174   EXPECT_EQ(0, strcmp(b.c_str(), "three\n"))
  175     << "actual value was '" << b.c_str() << "'";
  176 
  177   /* Make sure we also detect EOF. */
  178   ASSERT_EQ(1, b.read_line(fp));
  179   fclose (fp);
  180 }