Skip to content

Commit

Permalink
add zip utility (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan authored Feb 20, 2024
1 parent bcc8540 commit 2dca29b
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 40 deletions.
2 changes: 1 addition & 1 deletion inc/mkn/kul/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class Args {
std::stringstream ss;
for (std::size_t i = j + 1; i < argc; ++i) ss << argv[i] << " ";
rest_ = ss.str();
if(rest_.size()) rest_.pop_back();
if (rest_.size()) rest_.pop_back();
break; // assumes end
}
if (c.compare("---") == 0) KEXCEPT(Exception, "Illegal argument ---");
Expand Down
97 changes: 97 additions & 0 deletions inc/mkn/kul/zip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
Copyright (c) 2023, Philip Deegan.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of Philip Deegan 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
OWNER 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.
*/
#ifndef _MKN_KUL_IO_HPP_
#define _MKN_KUL_IO_HPP_

#include <iostream>
#include <tuple>

namespace mkn::kul {

template <std::size_t Is, typename Tuple>
struct Element {
using C = std::tuple_element_t<Is, Tuple>;
using T = typename C::value_type; // const containers do not have const value_types
using value_type = std::conditional_t<std::is_const_v<C>, T const&, T&>;
};

template <typename Tuple, std::size_t... Is>
constexpr auto tuple_element_value_type_refs_(Tuple, std::index_sequence<Is...> const&&)
-> std::tuple<typename Element<Is, Tuple>::value_type...>;

template <typename... Args>
auto constexpr tuple_element_value_type_refs()
-> decltype(tuple_element_value_type_refs_(std::tuple<Args...>{},
std::make_index_sequence<sizeof...(Args)>{}));

template <typename Tuple, std::size_t... Is>
constexpr auto get_tuple_element_value_type_refs_(Tuple& tup, std::size_t index,
std::index_sequence<Is...> const&&) {
return std::forward_as_tuple(std::get<Is>(tup)[index]...);
}
template <typename Tuple>
auto constexpr get_tuple_element_value_type_refs(Tuple& tuple, std::size_t index) {
return get_tuple_element_value_type_refs_(tuple, index,
std::make_index_sequence<std::tuple_size_v<Tuple>>{});
}

template <typename... Args>
struct Zipit {
auto operator*() { return get_tuple_element_value_type_refs(args, idx); }
bool operator==(Zipit const& that) const { return idx == that.idx; }
bool operator!=(Zipit const& that) const { return !(*(this) == that); }
auto& operator++() {
++idx;
return *this;
}

std::size_t idx = 0;
std::tuple<Args...>& args;
};

template <typename... Args>
struct Zipper {
using Iter = Zipit<Args...>;

auto begin() { return Iter{0, tup}; }
auto end() { return Iter{std::get<0>(tup).size(), tup}; }

std::tuple<Args...> tup;
};

template <typename... Args>
auto zip(Args&... args) {
return Zipper<Args...>{std::forward_as_tuple(args...)};
}

} // namespace mkn::kul

#endif /* _MKN_KUL_IO_HPP_ */
1 change: 1 addition & 0 deletions mkn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ profile:
parent: lib
inc: .
main: test/test.cpp
src: test/test
mode: none
dep: google.test
if_arg:
Expand Down
38 changes: 1 addition & 37 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,9 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "mkn/kul/assert.hpp"
#include "mkn/kul/cli.hpp"
#include "mkn/kul/io.hpp"
#include "mkn/kul/log.hpp"
#include "mkn/kul/math.hpp"
#include "mkn/kul/os.hpp"
#include "mkn/kul/proc.hpp"
#include "mkn/kul/threads.hpp"
#include "mkn/kul/span.hpp"
#include "mkn/kul/tuple.hpp"

#ifdef _WIN32
#define bzero ZeroMemory
#endif

auto tryCatch = [](std::vector<std::function<void()>> funcs, bool katch) {
for (const auto& func : funcs) try {
func();
ASSERT_TRUE(!katch);
} catch (const mkn::kul::Exception& e) {
if (!katch) KOUT(NON) << e.debug();
ASSERT_TRUE(katch);
}
};

#include "test/cli.ipp"
#include "test/except.ipp"
#include "test/io.ipp"
#include "test/math.ipp"
#include "test/os.ipp"
#include "test/proc.ipp"
#include "test/string.ipp"
#include "test/span.ipp"

int main(int argc, char* argv[]) {
KOUT(NON) << __FILE__;
::testing::InitGoogleMock(&argc, argv);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
4 changes: 4 additions & 0 deletions test/test/cli.ipp → test/test/cli.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/cli.hpp"

TEST(CLI_Test, ParseCommandLineArguments) {
std::vector<std::string> v;
mkn::kul::cli::asArgs("/path/to \"words in quotes\" words\\ not\\ in\\ quotes end", v);
Expand Down
4 changes: 4 additions & 0 deletions test/test/except.ipp → test/test/except.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/except.hpp"

TEST(Exception, String) {
std::string full = "FAILURE";
try {
Expand Down
8 changes: 6 additions & 2 deletions test/test/io.ipp → test/test/io.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/io.hpp"

TEST(IO_Test, ReadFileLine) {
mkn::kul::io::Reader r("LICENSE.md");
char const* c = r.readLine();
Expand All @@ -10,7 +14,7 @@ TEST(IO_Test, ReadFileLine) {
}
TEST(IO_Test, ReadFile) {
char c[20] = {0};
bzero(c, 20);
bzero(c);
mkn::kul::File file("LICENSE.md");
if (!file) KEXCEPT(mkn::kul::Exception, "ReadFile: FileNotFound: ") << file.full();
mkn::kul::io::Reader r("LICENSE.md");
Expand All @@ -37,7 +41,7 @@ TEST(IO_Test, ReadBinaryFileLine) {
}
TEST(IO_Test, ReadBinaryFile) {
char c[20] = {0};
bzero(c, 20);
bzero(c);
mkn::kul::io::BinaryReader r("LICENSE.md");
r.read(c, 20);
std::string s1 = c;
Expand Down
4 changes: 4 additions & 0 deletions test/test/math.ipp → test/test/math.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/math.hpp"

template <typename T>
void do_math() {
T beta = 2;
Expand Down
5 changes: 5 additions & 0 deletions test/test/os.ipp → test/test/os.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@


#include "test_common.hpp"

#include "mkn/kul/os.hpp"
#include "mkn/kul/proc.hpp"

TEST(OperatingSystemTests, HasRAMUsageSupport) {
ASSERT_TRUE(mkn::kul::this_proc::physicalMemory());
ASSERT_TRUE(mkn::kul::this_proc::virtualMemory());
Expand Down
4 changes: 4 additions & 0 deletions test/test/proc.ipp → test/test/proc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/proc.hpp"

TEST(Process_Test, LaunchProcess) {
mkn::kul::Process p("bash");
p << "-c"
Expand Down
5 changes: 5 additions & 0 deletions test/test/span.ipp → test/test/span.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/span.hpp"

TEST(Span, init) {
{
std::vector<double> v{1, 2, 3};
Expand Down
19 changes: 19 additions & 0 deletions test/test/string.ipp → test/test/string.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@


#include "test_common.hpp"

#include "mkn/kul/string.hpp"

TEST(StringOperations, SplitByChar) {
std::vector<std::string> v;
mkn::kul::String::SPLIT("split - by - char - dash", '-', v);
Expand Down Expand Up @@ -37,18 +41,21 @@ TEST(StringOperations, String_2_UInt16_t_invalid_tooLarge) {
}},
true);
}

TEST(StringOperations, String_2_UInt16_t_invalid_tooNegative) {
tryCatch(
{// tooNegative
[]() { mkn::kul::String::UINT16("-1"); }},
true);
}

TEST(StringOperations, String_2_UInt16_t_invalid_hasLetters) {
tryCatch({// contains letters * 3
[]() { mkn::kul::String::UINT16("a2c"); }, []() { mkn::kul::String::UINT16("1bc"); },
[]() { mkn::kul::String::UINT16("ab3"); }},
true);
}

TEST(StringOperations, String_2_UInt16_t_valid) {
tryCatch(
{// valid
Expand All @@ -64,6 +71,7 @@ TEST(StringOperations, String_2_Int16_t_invalid_tooLarge) {
}},
true);
}

TEST(StringOperations, String_2_Int16_t_invalid_tooNegative) {
tryCatch(
{// tooNegative
Expand All @@ -72,12 +80,14 @@ TEST(StringOperations, String_2_Int16_t_invalid_tooNegative) {
}},
true);
}

TEST(StringOperations, String_2_Int16_t_invalid_hasLetters) {
tryCatch({// contains letters * 3
[]() { mkn::kul::String::INT16("a2c"); }, []() { mkn::kul::String::INT16("1bc"); },
[]() { mkn::kul::String::INT16("ab3"); }},
true);
}

TEST(StringOperations, String_2_Int16_t_valid) {
tryCatch({// valid
[]() { mkn::kul::String::INT16("100"); }, []() { mkn::kul::String::INT16("-100"); }},
Expand All @@ -94,18 +104,21 @@ TEST(StringOperations, String_2_UInt32_t_invalid_tooLarge) {
}},
true);
}

TEST(StringOperations, String_2_UInt32_t_invalid_tooNegative) {
tryCatch(
{// tooNegative
[]() { mkn::kul::String::UINT32(std::to_string(-1)); }},
true);
}

TEST(StringOperations, String_2_UInt32_t_invalid_hasLetters) {
tryCatch({// contains letters * 3
[]() { mkn::kul::String::UINT32("a2c"); }, []() { mkn::kul::String::UINT32("1bc"); },
[]() { mkn::kul::String::UINT32("ab3"); }},
true);
}

TEST(StringOperations, String_2_UInt32_t_valid) {
tryCatch(
{// valid
Expand All @@ -123,6 +136,7 @@ TEST(StringOperations, String_2_Int32_t_invalid_tooLarge) {
}},
true);
}

TEST(StringOperations, String_2_Int32_t_invalid_tooNegative) {
tryCatch(
{// tooNegative
Expand All @@ -133,6 +147,7 @@ TEST(StringOperations, String_2_Int32_t_invalid_tooNegative) {
}},
true);
}

TEST(StringOperations, String_2_Int32_t_invalid_hasLetters) {
tryCatch({// contains letters * 3
[]() { mkn::kul::String::INT32("a2c"); }, []() { mkn::kul::String::INT32("1bc"); },
Expand All @@ -154,6 +169,7 @@ TEST(StringOperations, String_2_UInt64_t_invalid_tooLarge) {
}},
true);
}

TEST(StringOperations, String_2_UInt64_t_invalid_tooNegative) {
tryCatch(
{// tooNegative
Expand All @@ -163,12 +179,14 @@ TEST(StringOperations, String_2_UInt64_t_invalid_tooNegative) {
}},
true);
}

TEST(StringOperations, String_2_UInt64_t_invalid_hasLetters) {
tryCatch({// contains letters * 3
[]() { mkn::kul::String::UINT64("a2c"); }, []() { mkn::kul::String::UINT64("1bc"); },
[]() { mkn::kul::String::UINT64("ab3"); }},
true);
}

TEST(StringOperations, String_2_UInt64_t_valid) {
tryCatch(
{// valid
Expand All @@ -189,6 +207,7 @@ TEST(StringOperations, String_2_Int64_t_invalid) {
[]() { mkn::kul::String::INT64("ab3"); }},
true);
}

TEST(StringOperations, String_2_Int64_t_valid) {
tryCatch(
{// valid
Expand Down
Loading

0 comments on commit 2dca29b

Please sign in to comment.