Skip to content

Commit

Permalink
build(ci): test MSVC with github action
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtsHsu committed Feb 17, 2024
1 parent d99bd80 commit e489a9e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 34 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/build-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ on:
branches: [ "master" ]

jobs:
build:

test-gcc-clang:
runs-on: ubuntu-latest

steps:
- name: Install just
run: |
Expand All @@ -32,3 +30,14 @@ jobs:
- name: Build and Run Test with Clang 14
run: |
just t clang++-14
test-msvc:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: ilammy/msvc-dev-cmd@v1
with:
vsversion: 2022
- name: Build and run Test with MSVC
run: |
nmake /f nmakefile
52 changes: 24 additions & 28 deletions include/conststr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,9 +1194,7 @@ struct cstr {
constexpr auto insert(const_iterator iter,
const value_type &ch) const noexcept
-> cstr<N + Count, value_type, view_type> {
iter = std::max(iter, cbegin());
size_type pos = iter - cbegin();
if (pos > N) pos = N;
size_type pos = to_pos(iter);
if constexpr (Count == 0)
return *this;
else {
Expand Down Expand Up @@ -1241,10 +1239,7 @@ struct cstr {
*/
template <can_construct_cstr_t_from<value_type> Str>
constexpr auto insert(const_iterator iter, const Str &str) const noexcept {
iter = std::max(iter, cbegin());
size_type pos = iter - cbegin();
if (pos > N) pos = N;

size_type pos = to_pos(iter);
auto other = conststr::cstr(str);
constexpr size_type N2 = decltype(other)::size();
cstr<N + N2, value_type, view_type> ret{};
Expand Down Expand Up @@ -1370,10 +1365,7 @@ struct cstr {
constexpr auto replace(const_iterator first, const_iterator last,
const value_type &ch) const noexcept
-> cstr<N, value_type, view_type> {
first = std::max(first, cbegin());
last = std::min(last, cend());
size_type pos = first - cbegin();
size_type count = last - first;
auto [pos, count] = to_pos_len(first, last);
cstr<N, value_type, view_type> ret = *this;
size_type end = std::min(pos + count, N);
for (size_type i = pos; i < end; ++i) ret[i] = ch;
Expand Down Expand Up @@ -1413,10 +1405,7 @@ struct cstr {
constexpr auto replace(const_iterator first, const_iterator last,
const view_type &str) const noexcept
-> cstr<N, value_type, view_type> {
first = std::max(first, cbegin());
last = std::min(last, cend());
size_type pos = first - cbegin();
size_type count = last - first;
auto [pos, count] = to_pos_len(first, last);
return replace(pos, str, count);
}

Expand Down Expand Up @@ -1498,9 +1487,7 @@ struct cstr {
*/
constexpr auto erase(const_iterator iter) const noexcept
-> cstr<N - 1, value_type, view_type> {
iter = std::max(iter, cbegin());
size_type pos = iter - cbegin();
if (pos >= N) return pop();
size_type pos = to_pos(iter);
cstr<N - 1, value_type, view_type> ret{};
for (size_type i = 0; i < pos; ++i) ret[i] = _str[i];
for (size_type i = pos; i < N - 1; ++i) ret[i] = _str[i + 1];
Expand Down Expand Up @@ -1661,8 +1648,7 @@ struct cstr {
constexpr size_type find(const value_type &ch,
size_type pos = 0) const noexcept {
if (pos >= npos) return npos;
auto iter = std::find(cbegin() + pos, cend(), ch);
return iter - cbegin();
return to_pos(std::find(cbegin() + pos, cend(), ch));
}

/**
Expand All @@ -1674,9 +1660,8 @@ struct cstr {
constexpr size_type find(const view_type &str,
size_type pos = 0) const noexcept {
if (pos >= npos) return npos;
auto iter =
std::search(cbegin() + pos, cend(), str.cbegin(), str.cend());
return iter - cbegin();
return to_pos(
std::search(cbegin() + pos, cend(), str.cbegin(), str.cend()));
}

/**
Expand All @@ -1690,7 +1675,7 @@ struct cstr {
auto beg =
pos >= npos ? crbegin() : const_reverse_iterator(cbegin() + pos);
auto iter = std::find(beg, crend(), ch);
return iter.base() - cbegin() - 1;
return iter == crend() ? npos : to_pos(iter.base() - 1);
}

/**
Expand All @@ -1704,7 +1689,7 @@ struct cstr {
auto beg =
pos >= npos ? crbegin() : const_reverse_iterator(cbegin() + pos);
auto iter = std::search(beg, crend(), str.crbegin(), str.crend());
return iter.base() - cbegin() - str.size();
return iter == crend() ? npos : to_pos(iter.base() - str.size());
}

/**
Expand All @@ -1718,8 +1703,7 @@ struct cstr {
constexpr size_type find_if(UnaryPredicate p,
size_type pos = 0) const noexcept {
if (pos >= npos) return npos;
auto iter = std::find_if(cbegin() + pos, cend(), p);
return iter - cbegin();
return to_pos(std::find_if(cbegin() + pos, cend(), p));
}

/**
Expand All @@ -1735,7 +1719,7 @@ struct cstr {
auto beg =
pos >= npos ? crbegin() : const_reverse_iterator(begin() + pos);
auto iter = std::find_if(beg, crend(), p);
return iter.base() - cbegin() - 1;
return iter == crend() ? npos : to_pos(iter.base() - 1);
}

/**
Expand Down Expand Up @@ -1824,6 +1808,18 @@ struct cstr {
* you should not access it directly. Instead, you should use `begin()` and `end()`.
*/
value_type _str[N + 1];

private:
inline constexpr size_t to_pos(const_iterator iter) const noexcept {
return std::min(size_t(std::max(iter, cbegin()) - cbegin()), N);
}

inline constexpr std::tuple<size_t, size_t> to_pos_len(
const_iterator first, const_iterator last) const noexcept {
size_t first_pos = to_pos(first);
size_t last_pos = to_pos(last);
return {first_pos, std::max(first_pos, last_pos) - first_pos};
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/reflect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ constexpr decltype(auto) to_tuple(T &&t)
*/
template <typename T>
struct cptr {
const T *const ptr;
const T *ptr;
};

#if defined(__clang__)
Expand Down
10 changes: 10 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ outpath := "./build"
include := "./include"
default_cc := "/usr/bin/env g++"
cppflags := "-std=c++20 -Wall"
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]

alias b := build-tests
alias t := run-tests
alias nt := nmake-tests
alias c := clean

build-tests cc=default_cc:
#!/bin/bash
Expand All @@ -25,3 +28,10 @@ run-tests cc=default_cc: (build-tests cc)
for bin in {{ outpath }}/*; do
$bin;
done

nmake-tests:
nmake -f nmakefile
Get-ChildItem build -Filter *.exe | Foreach-Object { & $_.FullName }

clean:
rm -r ./build
17 changes: 17 additions & 0 deletions nmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

CC = cl
INCLUDES = ./include
CXXFLAGS = /std:c++20 /EHs /Wall /Fo./build/

EXES = $(**:.cpp=.exe)

.SUFFIXES: .cpp .exe
all: tests\*.cpp
mkdir build
$(MAKE) /f nmakefile $(EXES:tests=build)

{tests\}.cpp{build\}.exe:
$(CC) /I $(INCLUDES) $(CXXFLAGS) /Fe$@ $<

clean:
del /Q build
2 changes: 1 addition & 1 deletion tests/test-conststr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ int main() {
std::cout << __FILE__ ": all tests passed." << std::endl;

return 0;
}
}
2 changes: 1 addition & 1 deletion tests/test-reflect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ int main() {
std::cout << __FILE__ ": all tests passed." << std::endl;

return 0;
}
}

0 comments on commit e489a9e

Please sign in to comment.