Skip to content

Commit f469d59

Browse files
authored
Get GitHub actions working (#1)
1 parent 497495a commit f469d59

File tree

4 files changed

+97
-18
lines changed

4 files changed

+97
-18
lines changed

.github/workflows/build_cmake.yml

+5-16
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,14 @@ jobs:
2020
# We'll use this as our working directory for all subsequent commands
2121
run: cmake -E make_directory ${{runner.workspace}}/build
2222

23-
- name: Install conan
24-
shell: bash
25-
if: contains( matrix.os , "linux")
26-
run: |
27-
python3 -m pip install --upgrade pip setuptools
28-
python3 -m pip install conan
29-
source ~/.profile
30-
31-
- name: Install conan
32-
shell: bash
33-
if: contains(matrix.os, "windows")
34-
run: |
35-
pip install conan
3623

3724
- name: Install conan
3825
shell: bash
39-
if: contains(matrix.os, "macos")
4026
run: |
41-
pip install conan
27+
which pip3 && pip3 --version || pip --version
28+
which pip3 && pip3 install conan || pip install conan
29+
[[ -f ~/.profile ]] && source ~/.profile
30+
conan --version
4231
4332
- name: Configure CMake
4433
# Use a bash shell so we can use the same syntax for environment variable
@@ -51,7 +40,7 @@ jobs:
5140
#
5241
# We need to source the profile file to make sure conan is in PATH
5342
run: |
54-
source ~/.profile
43+
[[ -f ~/.profile ]] && source ~/.profile
5544
cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.buildtype}}
5645
5746
- name: Build

include/lambda_coroutines/lambda_coroutines.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef LAMBDA_COROUTINES_HPP
22
#define LAMBDA_COROUTINES_HPP
33

4+
#include <cstddef>
5+
#include <optional>
6+
#include <utility>
47

58
// because these are macros we cannot meaningfully use namespaces
69

@@ -28,14 +31,18 @@ constexpr void lambda_co_status_check(ParamType &value)
2831
switch (state_variable) { \
2932
default: \
3033
case 0:
34+
3135
#define lambda_co_return(return_value) \
3236
} \
3337
return return_value
38+
3439
#define lambda_co_yield(yield_value) \
3540
coroutine_lambda_internal_state_ref = LAMBDA_CO_CONSTEXPR_LINE; \
3641
return yield_value; \
3742
case LAMBDA_CO_CONSTEXPR_LINE:
3843

44+
#define lambda_co_end() }
45+
3946
template<typename Lambda>
4047
[[nodiscard]] constexpr auto range(
4148
Lambda lambda,

test/constexpr_tests.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <array>
2+
#include <utility>
3+
14
#include <catch2/catch.hpp>
25
#include "lambda_coroutines/lambda_coroutines.hpp"
36

@@ -32,7 +35,7 @@ constexpr std::array<unsigned long long, 5> get_first_5()
3235
return { fib().value(), fib().value(), fib().value(), fib().value(), fib().value() };
3336
}
3437

35-
TEST_CASE("lambda_coroutines are constexpr capable", "[lambda_coroutines]")
38+
TEST_CASE("lambda_coroutines are constexpr capable", "[constexpr]")
3639
{
3740
STATIC_REQUIRE(get_first_5()[4] == 3ULL);
3841
}

test/tests.cpp

+81-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
11
#include <catch2/catch.hpp>
2+
#include "lambda_coroutines/lambda_coroutines.hpp"
23

3-
// to-do non-constexpr tests
4+
TEST_CASE("Generate infinite sequence")
5+
{
6+
enum struct directions {
7+
Left,
8+
Right,
9+
Up,
10+
Down
11+
};
12+
13+
auto next_direction = [state=0]() mutable {
14+
lambda_co_begin(state);
15+
16+
while (true) {
17+
lambda_co_yield(directions::Left);
18+
lambda_co_yield(directions::Right);
19+
lambda_co_yield(directions::Up);
20+
lambda_co_yield(directions::Down);
21+
}
22+
23+
lambda_co_end();
24+
};
25+
26+
REQUIRE(next_direction() == directions::Left);
27+
REQUIRE(next_direction() == directions::Right);
28+
REQUIRE(next_direction() == directions::Up);
29+
REQUIRE(next_direction() == directions::Down);
30+
REQUIRE(next_direction() == directions::Left);
31+
REQUIRE(next_direction() == directions::Right);
32+
}
33+
34+
TEST_CASE("Cooperative multi tasking")
35+
{
36+
enum OpCodes : std::uint8_t {
37+
ADD = 0,
38+
STA = 1,
39+
NOP = 2
40+
};
41+
struct Machine {
42+
std::uint8_t PC{0};
43+
std::uint8_t A{0};
44+
std::array<uint8_t, 256> RAM{STA, 10, ADD, 15};
45+
};
46+
47+
Machine machine;
48+
49+
auto CPU = [state = 0, &machine, op = OpCodes::NOP]() mutable {
50+
lambda_co_begin(state);
51+
52+
while(true) {
53+
op = static_cast<OpCodes>(machine.RAM[machine.PC]);
54+
++machine.PC;
55+
if (op == OpCodes::STA) {
56+
lambda_co_yield();
57+
machine.A = machine.RAM[machine.PC++];
58+
lambda_co_yield();
59+
} else if (op == OpCodes::ADD) {
60+
lambda_co_yield();
61+
machine.A += machine.RAM[machine.PC++];
62+
lambda_co_yield();
63+
} else if (op == OpCodes::NOP) {
64+
lambda_co_yield();
65+
};
66+
}
67+
68+
lambda_co_end();
69+
};
70+
71+
CPU();
72+
REQUIRE(machine.PC == 1);
73+
REQUIRE(machine.A == 0);
74+
CPU();
75+
REQUIRE(machine.PC == 2);
76+
REQUIRE(machine.A == 10);
77+
CPU();
78+
REQUIRE(machine.PC == 3);
79+
REQUIRE(machine.A == 10);
80+
CPU();
81+
REQUIRE(machine.PC == 4);
82+
REQUIRE(machine.A == 25);
83+
}

0 commit comments

Comments
 (0)