-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example to demonstrate usage, and fix artifact name
- Loading branch information
Showing
7 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) !void { | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const include = b.path("include"); | ||
|
||
const lib = b.addStaticLibrary(.{ | ||
.name = "demo", | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
lib.addIncludePath(include); | ||
lib.addCSourceFiles(.{ | ||
.root = b.path("src"), | ||
.files = &.{"source.cpp"}, | ||
.flags = &CXXFLAGS, | ||
}); | ||
lib.linkLibCpp(); | ||
lib.installHeadersDirectory(include, "demo", .{ .include_extensions = &.{".hpp"} }); | ||
b.installArtifact(lib); | ||
|
||
{ // Test | ||
const test_step = b.step("test", "Run tests"); | ||
const test_exe = b.addExecutable(.{ .name = "test_demo", .target = target, .optimize = optimize }); | ||
const run_test = b.addRunArtifact(test_exe); | ||
|
||
const catch2_dep = b.dependency("catch2", .{ .target = target, .optimize = optimize }); | ||
const catch2_lib = catch2_dep.artifact("Catch2"); | ||
const catch2_main = catch2_dep.artifact("Catch2WithMain"); | ||
|
||
test_exe.addCSourceFiles(.{ .root = b.path("test"), .files = &.{"test.cpp"}, .flags = &CXXFLAGS }); | ||
test_exe.linkLibrary(lib); | ||
test_exe.linkLibrary(catch2_lib); | ||
test_exe.linkLibrary(catch2_main); | ||
test_step.dependOn(&run_test.step); | ||
} | ||
} | ||
|
||
const CXXFLAGS = .{ | ||
"--std=c++23", | ||
"-Wall", | ||
"-Wextra", | ||
"-Werror", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.{ | ||
.name = "cath2UsageDemo", | ||
.version = "1.0.0", | ||
.dependencies = .{ | ||
.catch2 = .{ | ||
.url = "..", | ||
.hash = "1220aef10cc88ccd919517ece637e54cfd7381c91ab9ee79a2d0d8791165ccd6bfff", | ||
}, | ||
}, | ||
.minimum_zig_version = "0.12.0", | ||
.paths = .{ | ||
"build.zig", | ||
"build.zig.zon", | ||
"src", | ||
"include", | ||
"test", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include <cstdint> // uintmax_t uint8_t | ||
|
||
uintmax_t fibonacci(uint8_t n, uintmax_t f0 = 0, uintmax_t f1 = 1) __attribute__((pure)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "header.hpp" | ||
|
||
auto fibonacci(uint8_t n, uintmax_t f0, uintmax_t f1) -> uintmax_t | ||
{ | ||
switch (n) { | ||
case 0: return f0; | ||
case 1: return f1; | ||
} | ||
return fibonacci(n-1, f1, f0 + f1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <demo/header.hpp> | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
TEST_CASE("Fibonacci") | ||
{ | ||
CHECK(fibonacci(7) == 13); | ||
CHECK(fibonacci(6, 1, 1) == 13); | ||
CHECK(fibonacci(5, 1, 2) == 13); | ||
|
||
CHECK(fibonacci(4, 5, 10) == 40); | ||
} |