Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "crazy_linker_line_reader.h"
      6 
      7 #include <minitest/minitest.h>
      8 
      9 #include "crazy_linker_system_mock.h"
     10 
     11 namespace crazy {
     12 
     13 static const char kFilePath[] = "/tmp/foo.txt";
     14 
     15 TEST(LineReader, EmptyConstructor) {
     16   LineReader reader;
     17   EXPECT_FALSE(reader.GetNextLine());
     18 }
     19 
     20 TEST(LineReader, EmptyFile) {
     21   SystemMock sys;
     22   sys.AddRegularFile(kFilePath, "", 0);
     23 
     24   LineReader reader(kFilePath);
     25   EXPECT_FALSE(reader.GetNextLine());
     26 }
     27 
     28 TEST(LineReader, SingleLineFile) {
     29   SystemMock sys;
     30   static const char kFile[] = "foo bar\n";
     31   static const size_t kFileSize = sizeof(kFile) - 1;
     32   sys.AddRegularFile(kFilePath, kFile, kFileSize);
     33 
     34   LineReader reader(kFilePath);
     35   EXPECT_TRUE(reader.GetNextLine());
     36   EXPECT_EQ(kFileSize, reader.length());
     37   EXPECT_MEMEQ(kFile, kFileSize, reader.line(), reader.length());
     38   EXPECT_FALSE(reader.GetNextLine());
     39 }
     40 
     41 TEST(LineReader, SingleLineFileUnterminated) {
     42   SystemMock sys;
     43   static const char kFile[] = "foo bar";
     44   static const size_t kFileSize = sizeof(kFile) - 1;
     45   sys.AddRegularFile(kFilePath, kFile, kFileSize);
     46 
     47   LineReader reader(kFilePath);
     48   EXPECT_TRUE(reader.GetNextLine());
     49   // The LineReader will add a newline to the last line.
     50   EXPECT_EQ(kFileSize + 1, reader.length());
     51   EXPECT_MEMEQ(kFile, kFileSize, reader.line(), reader.length() - 1);
     52   EXPECT_EQ('\n', reader.line()[reader.length() - 1]);
     53   EXPECT_FALSE(reader.GetNextLine());
     54 }
     55 
     56 TEST(LineReader, MultiLineFile) {
     57   SystemMock sys;
     58   static const char kFile[] =
     59       "This is a multi\n"
     60       "line text file that to test the crazy::LineReader class implementation\n"
     61       "And this is a very long text line to check that the class properly "
     62       "handles them, through the help of dynamic allocation or something. "
     63       "Yadda yadda yadda yadda. No newline";
     64   static const size_t kFileSize = sizeof(kFile) - 1;
     65   sys.AddRegularFile(kFilePath, kFile, kFileSize);
     66 
     67   LineReader reader(kFilePath);
     68 
     69   EXPECT_TRUE(reader.GetNextLine());
     70   EXPECT_MEMEQ("This is a multi\n", 16, reader.line(), reader.length());
     71 
     72   EXPECT_TRUE(reader.GetNextLine());
     73   EXPECT_MEMEQ(
     74       "line text file that to test the crazy::LineReader class "
     75       "implementation\n",
     76       88 - 17,
     77       reader.line(),
     78       reader.length());
     79 
     80   EXPECT_TRUE(reader.GetNextLine());
     81   EXPECT_MEMEQ(
     82       "And this is a very long text line to check that the class properly "
     83       "handles them, through the help of dynamic allocation or something. "
     84       "Yadda yadda yadda yadda. No newline\n",
     85       187 - 17,
     86       reader.line(),
     87       reader.length());
     88 
     89   EXPECT_FALSE(reader.GetNextLine());
     90 }
     91 
     92 }  // namespace crazy
     93