Skip to content

Commit

Permalink
add boost-tests (header-only)
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Sep 1, 2024
1 parent 1e4f860 commit abc86fa
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/zig-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
fetch-depth: 0
- uses: mlugg/setup-zig@v1
- name: Build Summary ${{ matrix.targets }}
run: zig build --summary all -freference-trace -Dtarget=${{ matrix.targets }}
run: zig build --build-file $PWD/tests/build.zig --summary all -freference-trace -Dtarget=${{ matrix.targets }}
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ pub fn build(b: *std.Build) !void {
for(boost_artifact.root_module.include_dirs.items) |include_dir| {
try exe.root_module.include_dirs.append(b.allocator, include_dir);
}
// your build
[exe|lib].linkLibrary(boost_artifact);
}
```

Expand Down
55 changes: 55 additions & 0 deletions tests/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const boost_dep = b.dependency("boost", .{
.target = target,
.optimize = optimize,
});

buildTests(b, .{
.target = target,
.optimize = optimize,
.files = &.{
"include_all.cc",
},
.name = "include_tests",
.dependency = boost_dep,
});
}
fn buildTests(b: *std.Build, options: struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
files: []const []const u8,
name: []const u8,
dependency: ?*std.Build.Dependency = null,
}) void {
const exe = b.addExecutable(.{
.name = options.name,
.target = options.target,
.optimize = options.optimize,
});

if (options.dependency) |dep| {
const artifact = dep.artifact("boost");
for (artifact.root_module.include_dirs.items) |include_dir| {
exe.root_module.include_dirs.append(b.allocator, include_dir) catch unreachable;
}
// exe.linkLibrary(artifact);
}

for (options.files) |file| {
exe.addCSourceFile(.{
.file = b.path(file),
});
}
if (exe.rootModuleTarget().abi != .msvc) {
exe.linkLibCpp();
} else {
exe.linkLibC();
}

b.installArtifact(exe);
}
10 changes: 10 additions & 0 deletions tests/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.{
.name = "tests",
.version = "0.0.0",
.dependencies = .{
.boost = .{
.path = "..",
},
},
.paths = .{""},
}
63 changes: 63 additions & 0 deletions tests/include_all.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <boost/algorithm/algorithm.hpp>
#include <boost/any.hpp>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/bind.hpp>
#include <boost/config.hpp>
#include <boost/hana.hpp>
#include <boost/multi_array.hpp>
#include <boost/pfr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <boost/variant.hpp>
#include <boost/version.hpp>

#include <iostream>

using namespace boost;

int main() {
std::cout << "Boost version " << BOOST_VERSION / 100000 << "."
<< BOOST_VERSION / 100 % 1000 << "." << BOOST_VERSION % 100
<< std::endl;

asio::io_context io_service;

// Example using boost::variant
variant<int, std::string, double> v;

v = 42;
std::cout << "Variant contains int: " << get<int>(v) << std::endl;

v = "Hello, Boost!";
std::cout << "Variant contains string: " << get<std::string>(v) << std::endl;

v = 3.14;
std::cout << "Variant contains double: " << get<double>(v) << std::endl;

// Using a visitor
struct visitor : public static_visitor<void> {
void operator()(int i) const {
std::cout << "Visited int: " << i << std::endl;
}
void operator()(const std::string &s) const {
std::cout << "Visited string: " << s << std::endl;
}
void operator()(double d) const {
std::cout << "Visited double: " << d << std::endl;
}
};

v = 100;
apply_visitor(visitor(), v);

v = "Boost Variant";
apply_visitor(visitor(), v);

v = 2.718;
apply_visitor(visitor(), v);

return 0;
}

0 comments on commit abc86fa

Please sign in to comment.