Skip to content

Commit

Permalink
char array input (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jl2922 authored Feb 21, 2018
1 parent 8f36c0f commit 332e81b
Show file tree
Hide file tree
Showing 33 changed files with 180 additions and 154 deletions.
44 changes: 8 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,19 @@
CXX := g++
CXX_WARNING_OPTIONS := -Wall -Wextra
CXXFLAGS := -std=c++11 -O3 $(CXX_WARNING_OPTIONS)
LDLIBS := -pthread -lprotobuf -lcapnp -lkj -lboost_serialization -lpthread
LDLIBS := -pthread -lpthread
SRC_DIR := src
BUILD_DIR := build
TEST_EXE := test.out

# Libraries.
BOOST_DIR := $(TOOLS_DIR)/boost
PROTOBUF_DIR := $(TOOLS_DIR)/protobuf
CAPNPROTO_DIR := $(TOOLS_DIR)/capnproto
CXXFLAGS := $(CXXFLAGS) -isystem $(BOOST_DIR)/include \
-isystem $(PROTOBUF_DIR)/include -isystem $(CAPNPROTO_DIR)/include
LDLIBS := -L $(BOOST_DIR)/lib -L $(PROTOBUF_DIR)/lib -L $(CAPNPROTO_DIR)/lib $(LDLIBS)

# Load Makefile.config if exists.
LOCAL_MAKEFILE := local.mk
ifneq ($(wildcard $(LOCAL_MAKEFILE)),)
include $(LOCAL_MAKEFILE)
endif

# Sources and intermediate objects.
BENCHMARK_DIR := $(SRC_DIR)/benchmark
PROTOBUF_SRC := $(BENCHMARK_DIR)/protobuf_benchmark.proto
PROTOBUF_COMPILED := $(BENCHMARK_DIR)/protobuf_benchmark.pb.h $(BENCHMARK_DIR)/protobuf_benchmark.pb.cc
CAPNPROTO_SRC := $(BENCHMARK_DIR)/capnproto_benchmark.capnp
CAPNPROTO_COMPILED_H := $(BENCHMARK_DIR)/capnproto_benchmark.capnp.h
CAPNPROTO_COMPILED_CC := $(BENCHMARK_DIR)/capnproto_benchmark.capnp.cc
CAPNPROTO_COMPILED_CXX := $(BENCHMARK_DIR)/capnproto_benchmark.capnp.c++
SRCS := $(shell find $(SRC_DIR) ! -name "*_test.cc" -name "*.cc")
TESTS := $(shell find $(SRC_DIR) -name "*_test.cc")
TESTS := $(shell find $(SRC_DIR) -name "*_test.cc" -not -path "$(SRC_DIR)/benchmark/*")
HEADERS := $(shell find $(SRC_DIR) -name "*.h")
OBJS := $(SRCS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o)
TEST_OBJS := $(TESTS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o)
Expand All @@ -45,28 +29,25 @@ TEST_MAIN_OBJ := $(BUILD_DIR)/gtest_main.o
TEST_CXXFLAGS := $(CXXFLAGS) -isystem $(GTEST_DIR)/include -isystem $(GMOCK_DIR)/include -pthread
TEST_LIB := $(BUILD_DIR)/libgtest.a

.PHONY: all test test_benchmark test_all test_build clean
.PHONY: all test benchmark test_all clean

.SUFFIXES:

all:
$(MAKE) test

test: test_build
test: $(TEST_EXE)
./$(TEST_EXE) --gtest_filter=-*LargeTest.*

test_benchmark: test_build
./$(TEST_EXE) --gtest_filter=*BenchmarkLargeTest.*
benchmark:
$(MAKE) -f benchmark.mk

test_all: test_build
test_all: $(TEST_EXE)
./$(TEST_EXE)

test_build: $(PROTOBUF_COMPILED) $(CAPNPROTO_COMPILED_H) $(CAPNPROTO_COMPILED_CC)
$(MAKE) $(TEST_EXE)

clean:
rm -rf $(BUILD_DIR)
rm -f ./$(TEST_EXE)
rm -f ./*.out

$(TEST_EXE): $(TEST_OBJS) $(OBJS) $(TEST_LIB)
$(CXX) $(TEST_CXXFLAGS) $(TEST_OBJS) $(OBJS) $(TEST_MAIN_SRC) $(TEST_LIB) -o $(TEST_EXE) $(LDLIBS)
Expand All @@ -85,12 +66,3 @@ $(TEST_LIB): $(BUILD_DIR)/gtest-all.o $(BUILD_DIR)/gmock-all.o

$(TEST_OBJS): $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cc $(HEADERS)
mkdir -p $(@D) && $(CXX) $(TEST_CXXFLAGS) -c $< -o $@

%.pb.h %.pb.cc: %.proto
protoc -I=$(@D) --cpp_out=$(@D) $<

%.capnp.c++ %.capnp.h: %.capnp
capnp compile -oc++ $<

%.capnp.cc: %.capnp.c++
cp $< $@
86 changes: 86 additions & 0 deletions benchmark.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Default options.
CXX := g++
CXX_WARNING_OPTIONS := -Wall -Wextra
CXXFLAGS := -std=c++11 -O3 $(CXX_WARNING_OPTIONS)
LDLIBS := -pthread -lprotobuf -lcapnp -lkj -lboost_serialization -lpthread
SRC_DIR := src
BUILD_DIR := build
TEST_EXE := benchmark.out

# Libraries.
BOOST_DIR := $(TOOLS_DIR)/boost
PROTOBUF_DIR := $(TOOLS_DIR)/protobuf
CAPNPROTO_DIR := $(TOOLS_DIR)/capnproto
CXXFLAGS := $(CXXFLAGS) -isystem $(BOOST_DIR)/include \
-isystem $(PROTOBUF_DIR)/include -isystem $(CAPNPROTO_DIR)/include
LDLIBS := -L $(BOOST_DIR)/lib -L $(PROTOBUF_DIR)/lib -L $(CAPNPROTO_DIR)/lib $(LDLIBS)

# Load Makefile.config if exists.
LOCAL_MAKEFILE := local.mk
ifneq ($(wildcard $(LOCAL_MAKEFILE)),)
include $(LOCAL_MAKEFILE)
endif

# Sources and intermediate objects.
BENCHMARK_DIR := $(SRC_DIR)/benchmark
PROTOBUF_SRC := $(BENCHMARK_DIR)/protobuf_benchmark.proto
PROTOBUF_COMPILED := $(BENCHMARK_DIR)/protobuf_benchmark.pb.h $(BENCHMARK_DIR)/protobuf_benchmark.pb.cc
CAPNPROTO_SRC := $(BENCHMARK_DIR)/capnproto_benchmark.capnp
CAPNPROTO_COMPILED_H := $(BENCHMARK_DIR)/capnproto_benchmark.capnp.h
CAPNPROTO_COMPILED_CC := $(BENCHMARK_DIR)/capnproto_benchmark.capnp.cc
CAPNPROTO_COMPILED_CXX := $(BENCHMARK_DIR)/capnproto_benchmark.capnp.c++
SRCS := $(shell find $(SRC_DIR) ! -name "*_test.cc" -name "*.cc")
TESTS := $(shell find $(SRC_DIR)/benchmark -name "*_test.cc")
HEADERS := $(shell find $(SRC_DIR) -name "*.h")
OBJS := $(SRCS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o)
TEST_OBJS := $(TESTS:$(SRC_DIR)/%.cc=$(BUILD_DIR)/%.o)

# Test related.
GTEST_DIR := gtest/googletest
GMOCK_DIR := gtest/googlemock
GTEST_ALL_SRC := ${GTEST_DIR}/src/gtest-all.cc
GMOCK_ALL_SRC := ${GMOCK_DIR}/src/gmock-all.cc
TEST_MAIN_SRC := ${GMOCK_DIR}/src/gmock_main.cc
TEST_MAIN_OBJ := $(BUILD_DIR)/gtest_main.o
TEST_CXXFLAGS := $(CXXFLAGS) -isystem $(GTEST_DIR)/include -isystem $(GMOCK_DIR)/include -pthread
TEST_LIB := $(BUILD_DIR)/libgtest.a

.PHONY: all benchmark test_build

.SUFFIXES:

all:
$(MAKE) -f benchmark.mk benchmark

benchmark: test_build
./$(TEST_EXE)

test_build: $(PROTOBUF_COMPILED) $(CAPNPROTO_COMPILED_H) $(CAPNPROTO_COMPILED_CC)
$(MAKE) -f benchmark.mk $(TEST_EXE)

$(TEST_EXE): $(TEST_OBJS) $(OBJS) $(TEST_LIB)
$(CXX) $(TEST_CXXFLAGS) $(TEST_OBJS) $(OBJS) $(TEST_MAIN_SRC) $(TEST_LIB) -o $(TEST_EXE) $(LDLIBS)

$(OBJS): $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cc $(HEADERS)
mkdir -p $(@D) && $(CXX) $(CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/gtest-all.o: $(GTEST_ALL_SRC)
mkdir -p $(@D) && $(CXX) $(TEST_CXXFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) -c $(GTEST_ALL_SRC) -o $@

$(BUILD_DIR)/gmock-all.o: $(GMOCK_ALL_SRC)
mkdir -p $(@D) && $(CXX) $(TEST_CXXFLAGS) -I$(GTEST_DIR) -I$(GMOCK_DIR) -c $(GMOCK_ALL_SRC) -o $@

$(TEST_LIB): $(BUILD_DIR)/gtest-all.o $(BUILD_DIR)/gmock-all.o
$(AR) $(ARFLAGS) $@ $(BUILD_DIR)/gtest-all.o $(BUILD_DIR)/gmock-all.o

$(TEST_OBJS): $(BUILD_DIR)/%.o: $(SRC_DIR)/%.cc $(HEADERS)
mkdir -p $(@D) && $(CXX) $(TEST_CXXFLAGS) -c $< -o $@

%.pb.h %.pb.cc: %.proto
protoc -I=$(@D) --cpp_out=$(@D) $<

%.capnp.c++ %.capnp.h: %.capnp
capnp compile -oc++ $<

%.capnp.cc: %.capnp.c++
cp $< $@
1 change: 1 addition & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ export PATH=$TOOLS_DIR/boost/bin:$PATH
export LD_LIBRARY_PATH=$TOOLS_DIR/boost/lib:$LD_LIBRARY_PATH

make test_all -j
make benchmark -j
5 changes: 1 addition & 4 deletions src/basic_type/basic_type.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef HPS_BASIC_TYPE_H_
#define HPS_BASIC_TYPE_H_
#pragma once

#include "float_serializer.h"
#include "int_serializer.h"
#include "string_serializer.h"
#include "uint_serializer.h"

#endif
5 changes: 1 addition & 4 deletions src/basic_type/float_serializer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_DOUBLE_SERIALIZER_H_
#define HPS_DOUBLE_SERIALIZER_H_
#pragma once

#include <iostream>
#include <type_traits>
Expand All @@ -22,5 +21,3 @@ class Serializer<T, B, typename std::enable_if<std::is_floating_point<T>::value,
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/basic_type/int_serializer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_INT_SERIALIZER_H_
#define HPS_INT_SERIALIZER_H_
#pragma once

#include <iostream>
#include <type_traits>
Expand Down Expand Up @@ -31,5 +30,3 @@ class Serializer<
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/basic_type/string_serializer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_STRING_SERIALIZER_H_
#define HPS_STRING_SERIALIZER_H_
#pragma once

#include <iostream>
#include <string>
Expand All @@ -26,5 +25,3 @@ class Serializer<std::string, B> {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/basic_type/uint_serializer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_UINT_SERIALIZER_H_
#define HPS_UINT_SERIALIZER_H_
#pragma once

#include <cassert>
#include <iostream>
Expand Down Expand Up @@ -49,5 +48,3 @@ class Serializer<T, B, typename std::enable_if<std::is_unsigned<T>::value, void>
};

} // namespace hps

#endif
6 changes: 2 additions & 4 deletions src/buffer/buffer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef HPS_BUFFER_H_
#define HPS_BUFFER_H_
#pragma once

#include "char_array_input_buffer.h"
#include "stream_input_buffer.h"
#include "stream_output_buffer.h"
#include "string_input_buffer.h"
#include "string_output_buffer.h"

#endif
30 changes: 30 additions & 0 deletions src/buffer/char_array_input_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <cstring>
#include "input_buffer.h"

namespace hps {

template <>
class InputBuffer<char*> {
public:
InputBuffer(const char* arr) : arr(arr) { pos = 0; }

void read(char* content, size_t length) {
strncpy(content, &arr[pos], length);
pos += length;
}

char read_char() {
const char ch = arr[pos];
pos++;
return ch;
}

private:
const char* const arr;

size_t pos;
};

} // namespace hps
5 changes: 1 addition & 4 deletions src/buffer/input_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_INPUT_BUFFER_H_
#define HPS_INPUT_BUFFER_H_
#pragma once

namespace hps {

Expand All @@ -10,5 +9,3 @@ class InputBuffer {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/buffer/output_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_OUTPUT_BUFFER_H_
#define HPS_OUTPUT_BUFFER_H_
#pragma once

namespace hps {

Expand All @@ -10,5 +9,3 @@ class OutputBuffer {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/buffer/stream.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#ifndef HPS_STREAM_H_
#define HPS_STREAM_H_
#pragma once

namespace hps {

class Stream {};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/buffer/stream_input_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_STREAM_INPUT_BUFFER_H_
#define HPS_STREAM_INPUT_BUFFER_H_
#pragma once

#include <cstring>
#include <iostream>
Expand Down Expand Up @@ -62,5 +61,3 @@ class InputBuffer<Stream> {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/buffer/stream_output_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_STREAM_OUTPUT_BUFFER_H_
#define HPS_STREAM_OUTPUT_BUFFER_H_
#pragma once

#include <cstring>
#include <iostream>
Expand Down Expand Up @@ -57,5 +56,3 @@ class OutputBuffer<Stream> {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/buffer/string_input_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_STRING_INPUT_BUFFER_H_
#define HPS_STRING_INPUT_BUFFER_H_
#pragma once

#include <string>
#include "input_buffer.h"
Expand Down Expand Up @@ -29,5 +28,3 @@ class InputBuffer<std::string> {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/buffer/string_output_buffer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_STRING_OUTPUT_BUFFER_H_
#define HPS_STRING_OUTPUT_BUFFER_H_
#pragma once

#include <string>
#include "output_buffer.h"
Expand Down Expand Up @@ -55,5 +54,3 @@ class OutputBuffer<std::string> {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/container/array_serializer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef HPS_ARRAY_SERIALIZER_H_
#define HPS_ARRAY_SERIALIZER_H_
#pragma once

#include <array>
#include <iostream>
Expand Down Expand Up @@ -84,5 +83,3 @@ class Serializer<std::array<bool, N>, B> {
};

} // namespace hps

#endif
5 changes: 1 addition & 4 deletions src/container/bit_filters.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef HPS_BIT_FILTERS_H_
#define HPS_BIT_FILTERS_H_
#pragma once

namespace hps {

constexpr char BIT_FILTERS[] = {1, 2, 4, 8, 16, 32, 64, static_cast<char>(128)};
}

#endif
Loading

0 comments on commit 332e81b

Please sign in to comment.