<commit_before>/*
  Zipios++ - a small C++ library that provides easy access to .zip files.
  Copyright (C) 2000-2015  <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/** \file
 *
 * Zipios++ unit tests for the ZipFile class.
 */

#include "catch_tests.hpp"

#include "zipios++/zipfile.hpp"
#include "zipios++/zipiosexceptions.hpp"

#include "src/dostime.h"

//#include <fstream>
//#include <memory>
//#include <vector>
//
//#include <unistd.h>
//#include <string.h>





SCENARIO("ZipFile with invalid paths", "[ZipFile] [FileCollection]")
{
    system("rm -rf tree"); // clean up, just in case
    size_t const start_count(rand() % 40 + 80);
    zipios_test::file_t tree(zipios_test::file_t::type_t::DIRECTORY, start_count, "tree");
    zipios_test::auto_unlink_t remove_zip("tree.zip");
    system("zip -r tree.zip tree >/dev/null");

    GIVEN("an empty directory collection")
    {
        zipios::ZipFile zf("tree.zip");

        // first, check that the object is setup as expected
        SECTION("verify that the object looks as expected")
        {
            REQUIRE(zf.isValid());
            REQUIRE_FALSE(zf.entries().empty());
            REQUIRE_FALSE(zf.getEntry("inexistant", zipios::FileCollection::MatchPath::MATCH));
            REQUIRE_FALSE(zf.getEntry("inexistant", zipios::FileCollection::MatchPath::IGNORE));
            REQUIRE_FALSE(zf.getInputStream("inexistant", zipios::FileCollection::MatchPath::MATCH));
            REQUIRE_FALSE(zf.getInputStream("inexistant", zipios::FileCollection::MatchPath::IGNORE));
            REQUIRE(zf.getName() == "tree.zip");
            REQUIRE(zf.size() == tree.size());
            zf.mustBeValid(); // not throwing

            {
                zipios::FileEntry::vector_t v(zf.entries());
                for(auto it(v.begin()); it != v.end(); ++it)
                {
                    zipios::FileEntry::pointer_t entry(*it);

                    // verify that our tree knows about this file
                    zipios_test::file_t::type_t t(tree.find(entry->getName()));
                    REQUIRE(t != zipios_test::file_t::type_t::UNKNOWN);

                    struct stat file_stats;
                    REQUIRE(stat(entry->getName().c_str(), &file_stats) == 0);

                    REQUIRE((*it)->getComment().empty());
                    //REQUIRE((*it)->getCompressedSize() == (*it)->getSize()); -- not too sure how we could verify this size in this case
                    //REQUIRE((*it)->getCrc() == ...); -- not too sure how to compute that right now, but once we have it we'll test it<commit_msg>Ameliorated the ZipFile test. It works better and test a lot more. Also includes an empty ZipFile test.<commit_after>/*
  Zipios++ - a small C++ library that provides easy access to .zip files.
  Copyright (C) 2000-2015  <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/** \file
 *
 * Zipios++ unit tests for the ZipFile class.
 */

#include "catch_tests.hpp"

#include "zipios++/zipfile.hpp"
#include "zipios++/zipiosexceptions.hpp"

#include "src/dostime.h"

//#include <fstream>
//#include <memory>
//#include <vector>
//
//#include <unistd.h>
//#include <string.h>





TEST_CASE("An Empty ZipFile", "[ZipFile] [FileCollection]")
{
    zipios::ZipFile zf;

    REQUIRE_FALSE(zf.isValid());
    REQUIRE_THROWS_AS(zf.entries().empty(), zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.getEntry("inexistant", zipios::FileCollection::MatchPath::MATCH), zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.getEntry("inexistant", zipios::FileCollection::MatchPath::IGNORE), zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.getInputStream("inexistant", zipios::FileCollection::MatchPath::MATCH), zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.getInputStream("inexistant", zipios::FileCollection::MatchPath::IGNORE), zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.getName() == "-", zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.size() == 0, zipios::InvalidStateException);
    REQUIRE_THROWS_AS(zf.mustBeValid(), zipios::InvalidStateException); // not throwing
}


SCENARIO("ZipFile with a working", "[ZipFile] [FileCollection]")
{
    GIVEN("a tree directory")
    {
        system("rm -rf tree"); // clean up, just in case
        size_t const start_count(rand() % 40 + 80);
        zipios_test::file_t tree(zipios_test::file_t::type_t::DIRECTORY, start_count, "tree");
        zipios_test::auto_unlink_t remove_zip("tree.zip");
        system("zip -r tree.zip tree >/dev/null");


        // first, check that the object is setup as expected
        WHEN("we load the zip file")
        {
            zipios::ZipFile zf("tree.zip");

            THEN("it is valid and includes all the files in the tree")
            {
                REQUIRE(zf.isValid());
                REQUIRE_FALSE(zf.entries().empty());
                REQUIRE_FALSE(zf.getEntry("inexistant", zipios::FileCollection::MatchPath::MATCH));
                REQUIRE_FALSE(zf.getEntry("inexistant", zipios::FileCollection::MatchPath::IGNORE));
                REQUIRE_FALSE(zf.getInputStream("inexistant", zipios::FileCollection::MatchPath::MATCH));
                REQUIRE_FALSE(zf.getInputStream("inexistant", zipios::FileCollection::MatchPath::IGNORE));
                REQUIRE(zf.getName() == "tree.zip");
                REQUIRE(zf.size() == tree.size());
                zf.mustBeValid(); // not throwing

                zipios::FileEntry::vector_t v(zf.entries());
                for(auto it(v.begin()); it != v.end(); ++it)
                {
                    zipios::FileEntry::pointer_t entry(*it);

                    // verify that our tree knows about this file
                    zipios_test::file_t::type_t t(tree.find(entry->getName()));
                    REQUIRE(t != zipios_test::file_t::type_t::UNKNOWN);

                    struct stat file_stats;
                    REQUIRE(stat(entry->getName().c_str(), &file_stats) == 0);

                    REQUIRE((*it)->getComment().empty());
                    //REQUIRE((*it)->getCompressedSize() == (*it)->getSize()); -- not too sure how we could verify this size in this case
                    //REQUIRE((*it)->getCrc() == ...); -- not too sure how to compute that right now, but once we have it we'll test it
{'fraction_non_alphanumeric': 0.13020765312785687, 'fraction_numerical': 0.007444168734491315, 'mean_word_length': 2.9947835159102767, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1170405', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2243, 'n_words': 631}