Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/small_world/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ coverage --combined_report=lcov
build:cov_mac --repo_env=GCOV=/Library/Developer/CommandLineTools/usr/bin/llvm-profdata
build:cov_mac --test_env=LLVM_COV=/Library/Developer/CommandLineTools/usr/bin/llvm-cov
build:cov_mac --copt=-ffile-compilation-dir=.

build:system-gcc --platforms=@rules_swiftnav//platforms:x86_64_linux_gcc11_system
18 changes: 18 additions & 0 deletions examples/small_world/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,37 @@ use_repo(
)

register_toolchains(
# llvm14
"@rules_swiftnav//cc/toolchains/llvm/aarch64-darwin:cc-toolchain-aarch64-darwin",
"@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-x86_64-linux",
# llvm20
"@rules_swiftnav//cc/toolchains/llvm20/aarch64-darwin:cc-toolchain-aarch64-darwin",
"@rules_swiftnav//cc/toolchains/llvm20/x86_64-darwin:cc-toolchain-x86_64-darwin",
"@rules_swiftnav//cc/toolchains/llvm20/aarch64-linux:cc-toolchain-aarch64-linux",
"@rules_swiftnav//cc/toolchains/llvm20/x86_64-linux:cc-toolchain-x86_64-linux",
# system gcc
"@rules_swiftnav//cc/toolchains/system_gcc/gcc11_x86_64-linux:system-cc-toolchain-x86_64-linux",
)

bazel_dep(name = "googletest", version = "1.16.0", dev_dependency = True)
bazel_dep(name = "lcov", version = "2.3.1", dev_dependency = True)

bazel_dep(name = "eigen", version = "5.0.0")

# Register the buildifier toolchain
bazel_dep(
name = "buildifier_prebuilt",
version = "8.2.0.2",
dev_dependency = True,
)

# bazel run @hedron_compile_commands//:refresh_all
# bazel run @hedron_compile_commands//:refresh_all -- --config=system-gcc
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
git_override(
module_name = "hedron_compile_commands",
commit = "4f28899228fb3ad0126897876f147ca15026151e",
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
# Replace the commit hash (above) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main).
# Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
)
4 changes: 3 additions & 1 deletion examples/small_world/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions examples/small_world/src/base_math/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
load("@rules_swiftnav//cc:defs.bzl", "UNIT", "swift_cc_library", "swift_cc_test")

swift_cc_library(
name = "base_math",
srcs = ["base_math.cc"],
hdrs = ["base_math.hpp"],
name = "add",
srcs = ["add.cc"],
hdrs = ["add.hpp"],
standard = 20,
visibility = ["//visibility:public"],
)

swift_cc_test(
name = "base_math_test",
srcs = ["base_math_test.cc"],
name = "add_test",
srcs = ["add_test.cc"],
type = UNIT,
deps = [
":base_math",
":add",
"@googletest//:gtest_main",
],
)

swift_cc_library(
name = "sign",
srcs = ["sign.cc"],
hdrs = ["sign.hpp"],
standard = 20,
visibility = ["//visibility:public"],
)

swift_cc_test(
name = "sign_test",
srcs = ["sign_test.cc"],
type = UNIT,
deps = [
":sign",
"@googletest//:gtest_main",
],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "base_math.hpp"
#include "add.hpp"

int add(int x, int y) {
return x + y;
Expand Down
6 changes: 6 additions & 0 deletions examples/small_world/src/base_math/add.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef ADD_HPP
#define ADD_HPP

int add(int x, int y);

#endif
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#include "base_math.hpp"
#include "add.hpp"
#include "gtest/gtest.h"

TEST(BaseMathTest, Add) {
EXPECT_EQ(add(1, 2), 3);
EXPECT_EQ(add(-1, 1), 0);
EXPECT_EQ(add(-1, -1), -2);
}

TEST(BaseMathTest, SignPlus) {
EXPECT_EQ(Math::sign(10), 1);
//EXPECT_EQ(Math::sign(-10), -1);
}
5 changes: 5 additions & 0 deletions examples/small_world/src/base_math/sign.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "sign.hpp"

int add(int x, int y) {
return x + y;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef BASE_MATH_HPP
#define BASE_MATH_HPP
#ifndef SIGN_HPP
#define SIGN_HPP

int add(int x, int y);

Expand Down
7 changes: 7 additions & 0 deletions examples/small_world/src/base_math/sign_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "sign.hpp"
#include "gtest/gtest.h"

TEST(BaseMathTest, SignPlus) {
EXPECT_EQ(Math::sign(10), 1);
//EXPECT_EQ(Math::sign(-10), -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I do not want to reach 100% coverage intentionally.

}
2 changes: 1 addition & 1 deletion examples/small_world/src/fibonacci/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ swift_cc_library(
srcs = ["fibonacci.cc"],
hdrs = ["fibonacci.hpp"],
visibility = ["//visibility:public"],
deps = ["//src/base_math"],
deps = ["//src/base_math:add"],
)

swift_cc_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/small_world/src/fibonacci/fibonacci.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "fibonacci.hpp"
#include "src/base_math/base_math.hpp"
#include "src/base_math/add.hpp"

int fibonacci(int n) {
if (n <= 1) {
Expand Down
4 changes: 2 additions & 2 deletions examples/small_world/src/old_folder_structure/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_swiftnav//cc:defs.bzl", "UNIT", "swift_cc_library", "swift_cc_test")
load("@rules_swiftnav//cc:defs.bzl", "UNIT", "swift_cc_test")

swift_cc_library(
cc_library(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this not use the swift macro?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to test the behavior of the toolchains, when the normal rules are used (mimic behavior from 3rd party libs)

name = "old_folder_structure",
srcs = ["src/old_folder_structure.cc"],
hdrs = ["include/old_folder_structure/old_folder_structure.hpp"],
Expand Down
21 changes: 21 additions & 0 deletions examples/small_world/src/use_external_dep/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@rules_swiftnav//cc:defs.bzl", "UNIT", "swift_cc_library", "swift_cc_test")

swift_cc_library(
name = "use_external_dep",
srcs = ["use_external_dep.cc"],
hdrs = ["use_external_dep.hpp"],
visibility = ["//visibility:public"],
deps = [
"@eigen",
],
)

swift_cc_test(
name = "use_external_dep_test",
srcs = ["use_external_dep_test.cc"],
type = UNIT,
deps = [
":use_external_dep",
"@googletest//:gtest_main",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "use_external_dep.hpp"

double compute_dot_product(const Eigen::Vector3d& a, const Eigen::Vector3d& b) {
return a.dot(b);
}

double compute_magnitude(const Eigen::Vector3d& v) {
return v.norm();
}
12 changes: 12 additions & 0 deletions examples/small_world/src/use_external_dep/use_external_dep.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef USE_EXTERNAL_DEP_HPP
#define USE_EXTERNAL_DEP_HPP

#include <Eigen/Dense>

// Compute the dot product of two 3D vectors using Eigen
double compute_dot_product(const Eigen::Vector3d& a, const Eigen::Vector3d& b);

// Compute the magnitude of a 3D vector using Eigen
double compute_magnitude(const Eigen::Vector3d& v);

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "use_external_dep.hpp"
#include "gtest/gtest.h"

TEST(UseExternalDepTest, DotProduct) {
Eigen::Vector3d a(1.0, 2.0, 3.0);
Eigen::Vector3d b(4.0, 5.0, 6.0);

// Expected dot product: 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
EXPECT_DOUBLE_EQ(compute_dot_product(a, b), 32.0);
}

TEST(UseExternalDepTest, DotProductOrthogonal) {
Eigen::Vector3d a(1.0, 0.0, 0.0);
Eigen::Vector3d b(0.0, 1.0, 0.0);

// Orthogonal vectors have dot product of 0
EXPECT_DOUBLE_EQ(compute_dot_product(a, b), 0.0);
}

TEST(UseExternalDepTest, Magnitude) {
Eigen::Vector3d v(3.0, 4.0, 0.0);

// Expected magnitude: sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5
EXPECT_DOUBLE_EQ(compute_magnitude(v), 5.0);
}

TEST(UseExternalDepTest, MagnitudeUnitVector) {
Eigen::Vector3d v(1.0, 0.0, 0.0);

// Unit vector has magnitude of 1
EXPECT_DOUBLE_EQ(compute_magnitude(v), 1.0);
}
Loading