Home | History | Annotate | Download | only in unittests
      1 //===- implTest.cpp -------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #include <mcld/Support/FileHandle.h>
     10 #include <mcld/Support/Path.h>
     11 #include <fcntl.h>
     12 #include <errno.h>
     13 #include "FileHandleTest.h"
     14 
     15 using namespace mcld;
     16 using namespace mcldtest;
     17 
     18 
     19 // Constructor can do set-up work for all test here.
     20 FileHandleTest::FileHandleTest()
     21 {
     22   // create testee. modify it if need
     23   m_pTestee = new FileHandle();
     24 }
     25 
     26 // Destructor can do clean-up work that doesn't throw exceptions here.
     27 FileHandleTest::~FileHandleTest()
     28 {
     29   delete m_pTestee;
     30 }
     31 
     32 // SetUp() will be called immediately before each test.
     33 void FileHandleTest::SetUp()
     34 {
     35 }
     36 
     37 // TearDown() will be called immediately after each test.
     38 void FileHandleTest::TearDown()
     39 {
     40 }
     41 
     42 //===----------------------------------------------------------------------===//
     43 // Testcases
     44 //===----------------------------------------------------------------------===//
     45 TEST_F(FileHandleTest, open_close) {
     46   mcld::sys::fs::Path path(TOPDIR);
     47   path.append("unittests/test.txt");
     48   ASSERT_TRUE(m_pTestee->open(path, FileHandle::ReadOnly));
     49   ASSERT_TRUE(m_pTestee->isOpened());
     50   ASSERT_TRUE(m_pTestee->isGood());
     51   ASSERT_TRUE(m_pTestee->isOwned());
     52 
     53   ASSERT_TRUE(27 == m_pTestee->size());
     54 
     55   ASSERT_TRUE(m_pTestee->close());
     56   ASSERT_FALSE(m_pTestee->isOpened());
     57   ASSERT_TRUE(m_pTestee->isGood());
     58 
     59   ASSERT_TRUE(0 == m_pTestee->size());
     60 }
     61 
     62 TEST_F(FileHandleTest, delegate_close) {
     63   mcld::sys::fs::Path path(TOPDIR);
     64   path.append("unittests/test.txt");
     65 
     66   int fd = ::open(path.native().c_str(), O_RDONLY);
     67 
     68   ASSERT_TRUE(m_pTestee->delegate(fd, FileHandle::ReadOnly));
     69   ASSERT_TRUE(m_pTestee->isOpened());
     70   ASSERT_TRUE(m_pTestee->isGood());
     71   ASSERT_FALSE(m_pTestee->isOwned());
     72 
     73   ASSERT_TRUE(27 == m_pTestee->size());
     74 
     75   ASSERT_TRUE(m_pTestee->close());
     76   ASSERT_FALSE(m_pTestee->isOpened());
     77   ASSERT_TRUE(m_pTestee->isGood());
     78   ASSERT_TRUE(m_pTestee->isOwned());
     79 
     80   ASSERT_TRUE(0 == m_pTestee->size());
     81 
     82   int close_result = ::close(fd);
     83   ASSERT_EQ(0, close_result);
     84 }
     85 
     86 TEST_F(FileHandleTest, fail_close) {
     87   mcld::sys::fs::Path path(TOPDIR);
     88   path.append("unittests/test.txt");
     89   ASSERT_TRUE(m_pTestee->open(path, FileHandle::ReadOnly));
     90   ASSERT_TRUE(m_pTestee->isOpened());
     91   ASSERT_TRUE(m_pTestee->isGood());
     92 
     93   ASSERT_TRUE(27 == m_pTestee->size());
     94 
     95   int close_result = ::close(m_pTestee->handler());
     96   ASSERT_EQ(0, close_result);
     97 
     98   ASSERT_FALSE(m_pTestee->close());
     99   ASSERT_FALSE(m_pTestee->isOpened());
    100   ASSERT_FALSE(m_pTestee->isGood());
    101 }
    102