text: /*
  Copyright(c) Dorin Duminica. All rights reserved.
  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright notice, 
  	 this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
  	 this list of conditions and the following disclaimer in the documentation
  	 and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its 
  	 contributors may be used to endorse or promote products derived from this
  	 software without specific prior written permission.
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
  
  #include "../ctests/ctests.h"
  #include "../iofile.h"
  
  CTEST_NEW("iofile_open_read/file_non_existing")
  {
      // create an iofile_t instance
      iofile_t *f = iofile_new("non_existing_file_name");
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for read, this should fail
      ASSERT_FALSE(iofile_open_read(f, &err));
      // free iofile_t instance
      iofile_free(f);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_NEW("iofile_open_read/file_exists")
  {
      // create an iofile_t instance
      iofile_t *f = iofile_new("./data/file_exists.txt");
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for read, this should succeed
      ASSERT_TRUE(iofile_open_read(f, &err));
      // free iofile_t instance
      iofile_free(f);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_NEW("iofile_open_write")
  {
      // sample data consts
      const char *KFILE_NAME = "./data/file_write.txt";
      const char *KSAMPLE_DATA = "iofile_open_write";
      const size_t KSAMPLE_DATA_LEN = strlen(KSAMPLE_DATA);
      // create an iofile_t instance
      iofile_t *f = iofile_new(KFILE_NAME);
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for write, this should succeed
      ASSERT_TRUE(iofile_open_write(f, &err));
      // write sample data
      size_t nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // delete the file
      remove(KFILE_NAME);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_NEW("iofile_open_wca")
  {
      // sample data consts
      const char *KFILE_NAME = "./data/file_wca.txt";
      const char *KSAMPLE_DATA = "iofile_open_wca";
      const size_t KSAMPLE_DATA_LEN = strlen(KSAMPLE_DATA);
      const char *KEXPECTED_DATA = "iofile_open_wcaiofile_open_wca";
      const size_t KEXPECTED_DATA_LEN = strlen(KEXPECTED_DATA);
      // create an iofile_t instance
      iofile_t *f = iofile_new(KFILE_NAME);
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for write, this should succeed
      ASSERT_TRUE(iofile_open_write(f, &err));
      // write sample data
      size_t nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for write-create-append
      ASSERT_TRUE(iofile_open_wca(f, &err));
      // write sample data at the end of the file
      nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // validate file close
      ASSERT_TRUE(iofile_close(f, &err));
      // open file for read
      ASSERT_TRUE(iofile_open_read(f, &err));
      // allocate a read buffer
      char *read_buf = (char *)calloc(1, KEXPECTED_DATA_LEN +1);
      // read data from file
      size_t nb_read = iofile_read_fn(f, &err, read_buf, KEXPECTED_DATA_LEN);
      // validate number of read bytes
      ASSERT_EQ(nb_read, KEXPECTED_DATA_LEN);
      // validate read data
      ASSERT_EQ(0, strcmp(KEXPECTED_DATA, read_buf));
      // free read buffer
      free(read_buf);
      // free iofile_t instance
      iofile_free(f);
      // delete file
      remove(KFILE_NAME);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_NEW("iofile_open_rw")
  {
      // sample data consts
      const char *KFILE_NAME = "./data/file_rw.txt";
      const char *KSAMPLE_DATA = "iofile_open_rw";
      const size_t KSAMPLE_DATA_LEN = strlen(KSAMPLE_DATA);
      // create an iofile_t instance
      iofile_t *f = iofile_new(KFILE_NAME);
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for write, this should succeed
      ASSERT_TRUE(iofile_open_write(f, &err));
      // write sample data
      size_t nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for read-write, this should succeed
      ASSERT_TRUE(iofile_open_rw(f, &err));
      // allocate read buffer
      char *read_buf = (char *)calloc(1, KSAMPLE_DATA_LEN +1);
      // read data from file
      size_t nb_read = iofile_read_fn(f, &err, read_buf, KSAMPLE_DATA_LEN);
      // validate number of read bytes
      ASSERT_EQ(nb_read, KSAMPLE_DATA_LEN);
      // validate read data
      ASSERT_EQ(0, strcmp(KSAMPLE_DATA, read_buf));
      // free read buffer
      free(read_buf);
      // free iofile_t instance
      iofile_free(f);
      // delete file
      remove(KFILE_NAME);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_NEW("iofile_open_rwt")
  {
      // sample data consts
      const char *KFILE_NAME = "./data/file_rwt.txt";
      const char *KSAMPLE_DATA = "iofile_open_rwt";
      const size_t KSAMPLE_DATA_LEN = strlen(KSAMPLE_DATA);
      const char *KEXPECTED_DATA = "open_rwt";
      const size_t KEXPECTED_DATA_LEN = strlen(KEXPECTED_DATA);
      // create an iofile_t instance
      iofile_t *f = iofile_new(KFILE_NAME);
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for write, this should succeed
      ASSERT_TRUE(iofile_open_write(f, &err));
      // write sample data
      size_t nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for read-write-trunc, this should succeed
      ASSERT_TRUE(iofile_open_rwt(f, &err));
      // write sample data
      nb_write = iofile_write_fn(f, &err, KEXPECTED_DATA, KEXPECTED_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KEXPECTED_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for read, this should succeed
      ASSERT_TRUE(iofile_open_read(f, &err));
      // allocate read buffer
      char *read_buf = (char *)calloc(1, KEXPECTED_DATA_LEN +1);
      // read data from file
      size_t nb_read = iofile_read_fn(f, &err, read_buf, KEXPECTED_DATA_LEN);
      // validate number of read bytes
      ASSERT_EQ(nb_read, KEXPECTED_DATA_LEN);
      // validate read data
      ASSERT_EQ(0, strcmp(KEXPECTED_DATA, read_buf));
      // free read buffer
      free(read_buf);
      // free iofile_t instance
      iofile_free(f);
      // delete file
      remove(KFILE_NAME);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_NEW("iofile_open_rwca")
  {
      // sample data consts
      const char *KFILE_NAME = "./data/file_rwca.txt";
      const char *KSAMPLE_DATA = "iofile_open_rwca";
      const size_t KSAMPLE_DATA_LEN = strlen(KSAMPLE_DATA);
      const char *KEXPECTED_DATA = "iofile_open_rwcaiofile_open_rwca";
      const size_t KEXPECTED_DATA_LEN = strlen(KEXPECTED_DATA);
      // create an iofile_t instance
      iofile_t *f = iofile_new(KFILE_NAME);
      // ioerror ref
      ioerror_t *err = NULL;
      // open file for read-write-create-append, this should succeed
      ASSERT_TRUE(iofile_open_rwca(f, &err));
      // write sample data
      size_t nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for read-write-create-append, this should succeed
      ASSERT_TRUE(iofile_open_rwca(f, &err));
      // write sample data
      nb_write = iofile_write_fn(f, &err, KSAMPLE_DATA, KSAMPLE_DATA_LEN);
      // validate number of written bytes
      ASSERT_EQ(nb_write, KSAMPLE_DATA_LEN);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for read-write-create-append, this should succeed
      ASSERT_TRUE(iofile_open_rwca(f, &err));
      // allocate read buffer
      char *read_buf = (char *)calloc(1, KEXPECTED_DATA_LEN +1);
      // read data from file
      size_t nb_read = iofile_read_fn(f, &err, read_buf, KEXPECTED_DATA_LEN);
      // validate number of read bytes
      ASSERT_EQ(nb_read, KEXPECTED_DATA_LEN);
      // validate read data
      ASSERT_EQ(0, strcmp(KEXPECTED_DATA, read_buf));
      // free read buffer
      free(read_buf);
      // free iofile_t instance
      iofile_free(f);
      // create a new iofile_t instance
      f = iofile_new(KFILE_NAME);
      // open file for read-write-create-append, this should succeed
      ASSERT_TRUE(iofile_open_rwca(f, &err));
      // seek to the middle of the file
      ASSERT_TRUE(iofile_seek_fn(f, &err, KSAMPLE_DATA_LEN, SEEK_SET));
      // allocate read bufffer
      read_buf = (char *)calloc(1, KSAMPLE_DATA_LEN +1);
      // read data from file at current position
      nb_read = iofile_read_fn(f, &err, read_buf, KSAMPLE_DATA_LEN);
      // validate number of read bytes
      ASSERT_EQ(nb_read, KSAMPLE_DATA_LEN);
      // validate read data
      ASSERT_EQ(0, strcmp(KSAMPLE_DATA, read_buf));
      // free read buffer
      free(read_buf);
      // free iofile_t instance
      iofile_free(f);
      // delete file
      remove(KFILE_NAME);
      // free ioerror_t instance if non-null
      ioerror_free(err);
  }
  
  CTEST_MAIN();
  
  
  