Skip to content

Add test for universal_formatter #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 28 additions & 0 deletions .github/workflows/c26-build-container.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: C++26 Build and Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build Docker image
run: |
docker build -t cpp26-app .

- name: Run tests in Docker container
run: |
docker run --rm cpp26-app /bin/bash -c "cd /workspace && cmake -B build"
2 changes: 1 addition & 1 deletion benchmarks/simpleparser/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void from_json(const JsonValue& j, T& obj) requires(simdjson::json_builder::User
}

// Using reflection to iterate over data members
[:simdjson::json_builder::expand(std::meta::nonstatic_data_members_of(^T)):] >> [&]<auto dm> {
[:simdjson::json_builder::expand(std::meta::nonstatic_data_members_of(^^T)):] >> [&]<auto dm> {
constexpr auto name = std::meta::identifier_of(dm);
auto it = j.object_value.find(std::string(reinterpret_cast<const char*>(name.data()), name.size()));
if (it != j.object_value.end()) {
Expand Down
6 changes: 3 additions & 3 deletions include/simdjson/json_builder/json_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ template <typename R> consteval auto expand(R range) {
for (auto r : range) {
args.push_back(std::meta::reflect_value(r));
}
return substitute(^__impl::replicator, args);
return substitute(^^__impl::replicator, args);
}
// end of workaround

Expand Down Expand Up @@ -88,7 +88,7 @@ template <class T>
constexpr void atom(string_builder &b, const T &t) {
int i = 0;
b.append('{');
[:expand(std::meta::nonstatic_data_members_of(^T)):] >> [&]<auto dm> {
[:expand(std::meta::nonstatic_data_members_of(^^T)):] >> [&]<auto dm> {
if (i != 0)
b.append(',');
constexpr auto v =
Expand All @@ -106,7 +106,7 @@ constexpr void atom(string_builder &b, const T &t) {
template <class Z> void fast_to_json_string(string_builder &b, const Z &z) {
int i = 0;
b.append('{');
[:expand(std::meta::nonstatic_data_members_of(^Z)):] >> [&]<auto dm> {
[:expand(std::meta::nonstatic_data_members_of(^^Z)):] >> [&]<auto dm> {
if (i != 0)
b.append(',');
constexpr auto v =
Expand Down
1 change: 1 addition & 0 deletions include/simdjson/json_builder/string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <string_view>
#include <vector>
#include <memory>

namespace simdjson {

Expand Down
15 changes: 8 additions & 7 deletions include/simdjson/json_builder/universal_formatter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SIMDJSON_SERIALIZATION_UNIVERSAL_FORMATTER_HPP
#define SIMDJSON_SERIALIZATION_UNIVERSAL_FORMATTER_HPP
#include "simdjson/json_builder/string_builder.hpp"
#include "simdjson/json_builder/string_builder.h"
#include "simdjson/json_builder/json_builder.h"
#include <experimental/meta>
#include <format>
#include <print>
Expand All @@ -22,16 +23,16 @@ struct universal_formatter {
first = false;
};

[:expand(bases_of(^T)):] >> [&]<auto base> {
[:expand(bases_of(^^T)):] >> [&]<auto base> {
delim();
sb.append(std::format("{}", (typename[:type_of(base):] const &)(t)));
};

[:expand(nonstatic_data_members_of(^T)):] >> [&]<auto mem> {
delim();
sb.append(std::format("\"{}\":{}", name_of(mem),
json_builder::atom(sb, t.[:mem:])));
};
// [:expand(nonstatic_data_members_of(^^T)):] >> [&]<auto mem> {
// delim();
// sb.append(std::format("\"{}\":{}", identifier_of(mem),
// json_builder::atom(sb, t.[:mem:])));
// };

sb.append('}');
}
Expand Down
12 changes: 11 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
CPMAddPackage(
NAME GTest
GITHUB_REPOSITORY google/googletest
VERSION 1.15.2
)

add_compile_options(-Wall -Wextra -Weffc++ -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion)

add_executable(user_profile_tests user_profile_tests.cpp)
target_link_libraries(user_profile_tests nlohmann_json::nlohmann_json simdjson::serialization simdjson::simdjson)
add_test(user_profile_tests user_profile_tests)
add_test(user_profile_tests user_profile_tests)

add_executable(universal_formatter_tests universal_formatter_tests.cpp)
target_link_libraries(universal_formatter_tests simdjson::serialization simdjson::simdjson GTest::gtest_main)
add_test(universal_formatter_tests universal_formatter_tests)
13 changes: 13 additions & 0 deletions tests/universal_formatter_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "simdjson/json_builder/universal_formatter.h"
#include <gtest/gtest.h>

TEST(UniversalFormatterTest, EmptyStruct) {
struct Empty {};

Empty obj;
simdjson::json_builder::universal_formatter universal_formatter;
simdjson::json_builder::string_builder string_builder;
universal_formatter.format(obj, string_builder);

EXPECT_EQ(string_builder.view(), "{}");
}
4 changes: 2 additions & 2 deletions tests/user_profile_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ template <auto B, auto E, typename F> constexpr void for_range(F &&f) {
}

template <typename T> consteval auto member_info(int n) {
return nonstatic_data_members_of(^T)[n];
return nonstatic_data_members_of(^^T)[n];
}

std::mt19937 rng(12345);
Expand Down Expand Up @@ -195,7 +195,7 @@ std::string nlohmann_serialize(const User &user) {
template <typename T>
bool compare(const T &user1, const T &user2, const std::string &json) {
bool result = true;
for_range<0, nonstatic_data_members_of(^T).size()>([&]<auto i>() {
for_range<0, nonstatic_data_members_of(^^T).size()>([&]<auto i>() {
constexpr auto mem = member_info<T>(i);
if constexpr (std::equality_comparable<typename[:type_of(mem):]>) {
if (user1.[:mem:] != user2.[:mem:]) {
Expand Down
Loading