text:
/*
 *  compression_tree_unittest.cpp
 *
 *  Created on: 15/10/2015
 *      Author: Karol Dzitkowski
 */

#include "test/unittest_base.hpp"
#include "core/cuda_array.hpp"

#include "tree/compression_tree.hpp"
#include "tree/compression_node.hpp"
#include "optimizer/path_generator.hpp"
#include <boost/make_shared.hpp>
#include <gtest/gtest.h>

#include "compression/default_encoding_factory.hpp"
#include "util/statistics/cuda_array_statistics.hpp"
#include "util/transform/cuda_array_transform.hpp"

namespace ddj {

class CompressionTreeTestBase : public UnittestBase
{
public:
	template<typename T>
	void CompressDecompressTest(CompressionTree& compressionTree, SharedCudaPtr<T> data);
};

class CompressionTreeTest : public CompressionTreeTestBase, public ::testing::WithParamInterface<int>
{
protected:
	virtual void SetUp() { _size = GetParam(); }
};

INSTANTIATE_TEST_CASE_P(
    CompressionTree_Test_Inst,
    CompressionTreeTest,
    ::testing::Values(10, 1000, 10000));

TEST_P(CompressionTreeTest, SimpleOneNodeTree_Delta_Int_Compress_NoException)
{
	CompressionTree tree;
	auto node = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	ASSERT_TRUE( tree.AddNode(node, 0) );
	tree.Compress(MoveSharedCudaPtr<int, char>(GetIntRandomData()));
}

template<typename T>
void CompressionTreeTestBase::CompressDecompressTest(CompressionTree& compressionTree, SharedCudaPtr<T> data)
{
	LOG4CPLUS_TRACE_FMT(_logger, "Testing data: %s ...", CudaArray().ToString(data->copy(10)).c_str());
	LOG4CPLUS_INFO_FMT(_logger, "Testing tree: %s", compressionTree.ToString().c_str());
	auto dataToCompress = CastSharedCudaPtr<T, char>(data);
	auto compressed = compressionTree.Compress(dataToCompress);

	CompressionTree decompressionTree;
	auto decompressed = decompressionTree.Decompress(compressed);

	auto expected = data;
	auto actual = CastSharedCudaPtr<char, T>(decompressed);

	ASSERT_EQ(expected->size(), actual->size());
	EXPECT_TRUE( CudaArray().Compare(expected, actual) );

	LOG4CPLUS_INFO(_logger, "Compression ratio: " << compressionTree.GetCompressionRatio());
}

TEST_P(CompressionTreeTest, SimpleOneNodeTree_Delta_Int_Compress_Decompress)
{
	CompressionTree compressionTree;
	auto node = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto leafNode = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
	node->AddChild(leafNode);
	ASSERT_TRUE( compressionTree.AddNode(node, 0) );
	CompressDecompressTest<int>(compressionTree, GetIntRandomData());
}

TEST_P(CompressionTreeTest, SimpleOneNodeTree_Gfc_Float_Compress_Decompress)
{
	CompressionTree compressionTree;
	auto node = boost::make_shared<CompressionNode>(boost::make_shared<GfcEncodingFactory>(DataType::d_float));
	ASSERT_TRUE( compressionTree.AddNode(node, 0) );
	compressionTree.Fix();
	CompressDecompressTest<int>(compressionTree, GetIntRandomData());
}

TEST_P(CompressionTreeTest, SimpleTwoNodeTree_DeltaAndScale_Int_Compress_Decompress)
{
	CompressionTree compressionTree;
	auto node = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto secondNode = boost::make_shared<CompressionNode>(boost::make_shared<ScaleEncodingFactory>(DataType::d_int));
	auto leafNode = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
	secondNode->AddChild(leafNode);
	node->AddChild(secondNode);
	ASSERT_TRUE( compressionTree.AddNode(node, 0) );
	CompressDecompressTest<int>(compressionTree, GetIntRandomData());
}

//     DICT
//    /    \
// SCALE   DELTA
//   |
// DELTA
TEST_P(CompressionTreeTest, ComplexTree_Dict_Compress_Decompress)
{
    CompressionTree compressionTree;
    auto root = boost::make_shared<CompressionNode>(boost::make_shared<DictEncodingFactory>(DataType::d_int));
	auto right = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto left = boost::make_shared<CompressionNode>(boost::make_shared<ScaleEncodingFactory>(DataType::d_int));
    auto leftChild = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto noneLeft = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
    auto noneRight = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
    leftChild->AddChild(noneLeft);
    left->AddChild(leftChild);
    root->AddChild(left);
    right->AddChild(noneRight);
    root->AddChild(right);
	ASSERT_TRUE( compressionTree.AddNode(root, 0) );
	CompressDecompressTest<int>(compressionTree, GetIntRandomData());
}

//         PATCH
//        /     \
//      DICT   DELTA
//     /   \
//  DELTA SCALE
TEST_P(CompressionTreeTest, ComplexTree_Patch_Compress_Decompress)
{
	CompressionTree compressionTree;
	auto pef = new PatchEncodingFactory<int>(DataType::d_int, PatchType::outside);
	auto root = boost::make_shared<CompressionNode>(boost::shared_ptr<PatchEncodingFactory<int>>(pef));
	auto right = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto left = boost::make_shared<CompressionNode>(boost::make_shared<DictEncodingFactory>(DataType::d_int));
	auto leftChildLeft = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto leftChildRight = boost::make_shared<CompressionNode>(boost::make_shared<ScaleEncodingFactory>(DataType::d_int));
	auto leaf1 = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
	auto leaf2 = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
	auto leaf3 = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));

	leftChildLeft->AddChild(leaf1);
	leftChildRight->AddChild(leaf2);
	right->AddChild(leaf3);
	left->AddChild(leftChildLeft);
	left->AddChild(leftChildRight);
	root->AddChild(left);
	root->AddChild(right);

	ASSERT_TRUE( compressionTree.AddNode(root, 0) );
	CompressDecompressTest<int>(compressionTree, GetIntRandomData());
}

//		SCALE
//		  |
//	    DELTA
//	      |
//	    PATCH
//	   /	 \
//	  AFL	NONE
TEST_P(CompressionTreeTest, ComplexTree_Delta_Scale_Patch_Afl_RealData_Time_Compress_Decompress)
{
	CompressionTree compressionTree;
	auto data = CudaArrayTransform().Cast<time_t, int>(GetTsIntDataFromTestFile());
	auto pef = new PatchEncodingFactory<int>(DataType::d_int, PatchType::lower);
	auto root = boost::make_shared<CompressionNode>(boost::make_shared<ScaleEncodingFactory>(DataType::d_int));
	auto delta = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto patch = boost::make_shared<CompressionNode>(boost::shared_ptr<PatchEncodingFactory<int>>(pef));
	auto afl = boost::make_shared<CompressionNode>(boost::make_shared<AflEncodingFactory>(DataType::d_int));
	auto leaf1 = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
	auto leaf2 = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));

	afl->AddChild(leaf1);
	patch->AddChild(afl);
	patch->AddChild(leaf2);
	delta->AddChild(patch);
	root->AddChild(delta);

	ASSERT_TRUE( compressionTree.AddNode(root, 0) );

	auto compressed = compressionTree.Compress(CastSharedCudaPtr<int, char>(data));
	CompressionTree decompressionTree;
	auto decompressed = decompressionTree.Decompress(compressed);
	auto expected = data;
	auto actual = MoveSharedCudaPtr<char, int>(decompressed);

	//	printf("size before compression = %d\n", data->size()*sizeof(int));
	//	printf("size after comrpession = %d\n", compressed->size()*sizeof(char));

	ASSERT_EQ(expected->size(), actual->size());
	EXPECT_TRUE( CudaArray().Compare(expected, actual) );
}

//		SCALE
//		  |
//		 RLE
//		  |
//		DELTA
//		  |
//		UNIQUE
TEST_P(CompressionTreeTest, ComplexTree_Scale_Rle_Delta_Unique_RealData_Time_Compress_Decompress)
{
	CompressionTree compressionTree;
	auto data = CudaArrayTransform().Cast<time_t, int>(GetTsIntDataFromTestFile());
	auto scale = boost::make_shared<CompressionNode>(boost::make_shared<ScaleEncodingFactory>(DataType::d_int));
	auto delta = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto rle = boost::make_shared<CompressionNode>(boost::make_shared<RleEncodingFactory>(DataType::d_int));
	auto unique = boost::make_shared<CompressionNode>(boost::make_shared<UniqueEncodingFactory>(DataType::d_int));
	auto leaf = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));

	unique->AddChild(leaf);
	delta->AddChild(unique);
	rle->AddChild(delta);
	scale->AddChild(delta);

	ASSERT_TRUE( compressionTree.AddNode(scale, 0) );

	auto compressed = compressionTree.Compress(CastSharedCudaPtr<int, char>(data));
	CompressionTree decompressionTree;
	auto decompressed = decompressionTree.Decompress(compressed);

	//	HelperPrint::PrintSharedCudaPtr(data, "data");
	//	HelperPrint::PrintSharedCudaPtr(CastSharedCudaPtr<char, int>(decompressed), "decompressed");

	auto expected = data;
	auto actual = MoveSharedCudaPtr<char, int>(decompressed);

	//	printf("size before compression = %d\n", data->size()*sizeof(int));
	//	printf("size after comrpession = %d\n", compressed->size()*sizeof(char));

	ASSERT_EQ(expected->size(), actual->size());
	EXPECT_TRUE( CudaArray().Compare(expected, actual) );
}

//		DELTA
//		  |
//		 AFL
TEST_P(CompressionTreeTest, SimpleTree_Delta_Afl_PredictSizeAfterCompression_RandomInt)
{
	CompressionTree compressionTree;
	auto data = CudaArrayTransform().Cast<time_t, int>(GetTsIntDataFromTestFile());
	auto delta = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto afl = boost::make_shared<CompressionNode>(boost::make_shared<AflEncodingFactory>(DataType::d_int));
	auto leaf = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));

	afl->AddChild(leaf);
	delta->AddChild(afl);

	ASSERT_TRUE( compressionTree.AddNode(delta, 0) );

	auto compressed = compressionTree.Compress(CastSharedCudaPtr<int, char>(data));

	auto expected = compressed->size();
	auto actual = compressionTree.GetPredictedSizeAfterCompression(
			CastSharedCudaPtr<int, char>(data), DataType::d_int);

	EXPECT_EQ(expected, actual);
}

//		PATCH
//	   /     \
//	 AFL     AFL
TEST_P(CompressionTreeTest, DISABLED_SimpleTree_Patch_Afl_RandomInt_BigSize)
{
	CompressionTree compressionTree;
	auto data =
			CastSharedCudaPtr<int, char>(
				CudaArrayGenerator().GenerateRandomIntDeviceArray(1<<20, 10, 1000));

	auto patch = boost::make_shared<CompressionNode>(DefaultEncodingFactory().Get(EncodingType::patch, DataType::d_int));
	auto aflLeft = boost::make_shared<CompressionNode>(boost::make_shared<AflEncodingFactory>(DataType::d_int));
	auto aflRight = boost::make_shared<CompressionNode>(boost::make_shared<AflEncodingFactory>(DataType::d_int));
	auto leafLeft = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));
	auto leafRight = boost::make_shared<CompressionNode>(boost::make_shared<NoneEncodingFactory>(DataType::d_int));

	aflLeft->AddChild(leafLeft);
	aflRight->AddChild(leafRight);
	patch->AddChild(aflLeft);
	patch->AddChild(aflRight);
	ASSERT_TRUE( compressionTree.AddNode(patch, 0) );

	auto compressed = compressionTree.Compress(data);
	CompressionTree decompressionTree;
	auto decompressed = decompressionTree.Decompress(compressed);
	auto expected = CastSharedCudaPtr<char, int>(data);
	auto actual = MoveSharedCudaPtr<char, int>(decompressed);

	//	printf("size before compression = %d\n", data->size()*sizeof(int));
	//	printf("size after comrpession = %d\n", compressed->size()*sizeof(char));
	//	HelperPrint::PrintSharedCudaPtr(expected, "expected");
	//	HelperPrint::PrintSharedCudaPtr(actual, "actual");

	ASSERT_EQ(expected->size(), actual->size());
	EXPECT_TRUE( CudaArray().Compare(expected, actual) );
}

//		SCALE
//		  |
//	    DELTA
//	      |
//	    PATCH
//	   /	 \
//	  AFL	AFL
TEST_P(CompressionTreeTest, UpdateStatistics_ComplexTree)
{
	auto stats = CompressionStatistics::make_shared(5);
	CompressionTree compressionTree(stats);

	auto data = CudaArrayTransform().Cast<time_t, int>(GetTsIntDataFromTestFile());
	auto pef = new PatchEncodingFactory<int>(DataType::d_int, PatchType::lower);
	auto root = boost::make_shared<CompressionNode>(boost::make_shared<ScaleEncodingFactory>(DataType::d_int));
	auto delta = boost::make_shared<CompressionNode>(boost::make_shared<DeltaEncodingFactory>(DataType::d_int));
	auto patch = boost::make_shared<CompressionNode>(boost::shared_ptr<PatchEncodingFactory<int>>(pef));
	auto afl_left = boost::make_shared<CompressionNode>(boost::make_shared<AflEncodingFactory>(DataType::d_int));
	auto afl_right = boost::make_shared<CompressionNode>(boost::make_shared<AflEncodingFactory>(DataType::d_int));

	patch->AddChild(afl_left);
	patch->AddChild(afl_right);
	delta->AddChild(patch);
	root->AddChild(delta);

	ASSERT_TRUE( compressionTree.AddNode(root, 0) );
	compressionTree.Fix();

	auto compressed = compressionTree.Compress(CastSharedCudaPtr<int, char>(data));
	CompressionTree decompressionTree;
	auto decompressed = decompressionTree.Decompress(compressed);
	auto expected = data;
	auto actual = MoveSharedCudaPtr<char, int>(decompressed);

	//	printf("size before compression = %lu\n", data->size()*sizeof(int));
	//	printf("size after comrpession = %lu\n", compressed->size()*sizeof(char));
	//	stats->Print();

	EXPECT_EQ(EncodingType::scale, stats->GetAny(0).type.first);
	EXPECT_EQ(EncodingType::delta, stats->GetAny(0).type.second);
	EXPECT_EQ(EncodingType::patch, stats->GetAny(6).type.first);
	EXPECT_EQ(EncodingType::afl, stats->GetAny(6).type.second);

	ASSERT_EQ(expected->size(), actual->size());
	EXPECT_TRUE( CudaArray().Compare(expected, actual) );
}

//			PATCH
//			/	\
//		CONST	FloatToInt
//					|
//				   RLE
//				  /   \
//			  CONST   SCALE
//						|
//					   AFL
TEST_F(CompressionTreeTestBase, FakeDataPatternA_Float_GoodTree)
{
	Path path
	{
		EncodingType::patch,
			EncodingType::constData, EncodingType::none,
			EncodingType::floatToInt,
				EncodingType::rle,
					EncodingType::constData, EncodingType::none,
					EncodingType::scale, EncodingType::afl, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);

	auto floatData = GetFakeDataWithPatternA<float>(0, 1e2, 1.5f, -10.0f, 1e6f, 1e6);
	CompressDecompressTest<float>(tree, floatData);
}

//	DELTA -> FLOAT_TO_INT -> AFL -> NONE
TEST_P(CompressionTreeTest, RandomFloats_WithPrecision_3_Delta_FloatToInt_Afl)
{
	Path path {
		EncodingType::delta,
			EncodingType::floatToInt,
				EncodingType::afl, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);
	auto floatData = GetFloatRandomDataWithMaxPrecision(3);
	CompressDecompressTest<float>(tree, floatData);
}

//	SCALE -> DELTA -> GFC -> 2*NONE
TEST_P(CompressionTreeTest, RandomFloats_WithPrecision_3_Scale_Delta_Gfc)
{
	Path path {
		EncodingType::scale,
			EncodingType::delta,
				EncodingType::gfc, EncodingType::none, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);
	auto floatData = GetFloatRandomDataWithMaxPrecision(3);
	CompressDecompressTest<float>(tree, floatData);
}

// GFC -> 2*NONE
TEST_F(CompressionTreeTestBase, FakeDataPatternA_Float_WithPrecision_3_OnlyGfc)
{
	Path path {
		EncodingType::gfc, EncodingType::none, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);
	auto floatData = GetFakeDataWithPatternA<float>(0, 1e2, 1.5f, -10.0f, 1e6f, 1e6);
	CompressDecompressTest<float>(tree, floatData);
}

// DICT -> 2*NONE
TEST_F(CompressionTreeTestBase, FakeDataPatternA_Float_WithPrecision_3_OnlyDict)
{
	Path path {
		EncodingType::dict, EncodingType::none, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);
	auto floatData = GetFakeDataWithPatternA<float>(0, 1e2, 1.5f, -10.0f, 1e6f, 1e6);
	CompressDecompressTest<float>(tree, floatData);
}

//			 DICT
//			/	 \
//	   SCALE     DELTA
//		|			|
//	  DELTA		FLOAT_TO_INT
//		|		    |
//	   GFC	       AFL
TEST_F(CompressionTreeTestBase, FakeDataPatternA_Float_TreeWith_Dict_And_FloatToInt)
{
	Path path
	{
		EncodingType::dict,
			EncodingType::scale, EncodingType::delta,
				EncodingType::gfc, EncodingType::none, EncodingType::none,
			EncodingType::delta, EncodingType::floatToInt,
				EncodingType::afl, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);
	auto floatData = GetFakeDataWithPatternA<float>(0, 1e2, 1.5f, -10.0f, 1e6f, 1e6);
	CompressDecompressTest<float>(tree, floatData);
}

TEST_F(CompressionTreeTestBase, FakeDataPatternA_Float_SpecialCaseWithDict_CompressedBy_ScaleDelta)
{
	auto floatData = GetFakeDataWithPatternA<float>(0, 1e2, 1.5f, -10.0f, 1e6f, 1e6);
	auto data = CastSharedCudaPtr<float, char>(floatData);
	auto dict = boost::make_shared<CompressionNode>(boost::make_shared<DictEncodingFactory>(DataType::d_float));
	auto result = dict->Compress(data);

	Path path {
		EncodingType::scale, EncodingType::delta, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_float);
	auto compressed = tree.Compress(result[1]);
	auto decompressed = tree.Decompress(compressed);

	auto expected = CastSharedCudaPtr<char, float>(result[1]);
	auto actual = CastSharedCudaPtr<char, float>(decompressed);

	ASSERT_EQ(expected->size(), actual->size());
	EXPECT_TRUE( CudaArray().Compare(expected, actual) );

	// HelperPrint::PrintSharedCudaPtr(expected, "expected");
	// HelperPrint::PrintSharedCudaPtr(actual, "actual");
}

//				DELTA
// 				  |
// 				PATCH
// 			   /	 \
// 			DELTA	DELTA
// 			  |		  |
//			 AFL	 AFL
TEST_F(CompressionTreeTestBase, RealTSData_Time_Delta_Patch_Delta_Afl)
{
	Path path
	{
		EncodingType::delta,
			EncodingType::patch,
				EncodingType::delta,
					EncodingType::afl, EncodingType::none,
				EncodingType::delta,
					EncodingType::afl, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_time);
	auto timeData = GetTsIntDataFromTestFile();
	CompressDecompressTest<time_t>(tree, timeData);
}

// 			DELTA
//			  |
//			 RLE
//			/	\
//		   AFL  AFL
TEST_F(CompressionTreeTestBase, GoodTreeForPatternB_Second)
{
	int N = 25;
	int SIZE = GetSize();
	Path path
	{
		EncodingType::delta,
			EncodingType::rle,
				EncodingType::afl, EncodingType::none,
				EncodingType::afl, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_int);
	for(int i = 0; i < N; i++)
	{
		auto data = GetFakeDataWithPatternB<int>(i, 3*SIZE, -1e4, 1e4, SIZE/2);
		CompressDecompressTest<int>(tree, data);
	}
}

//			PATCH
//         /	 \
//		DELTA	DELTA
//		  |		  |
//		SCALE   SCALE
//		  |		  |
//		 AFL	 AFL
TEST_F(CompressionTreeTestBase, GoodTreeForPatternB_First)
{
	int N = 25;
	int SIZE = GetSize();
	Path path
	{
		EncodingType::patch,
			EncodingType::delta, EncodingType::scale,
				EncodingType::afl, EncodingType::none,
			EncodingType::delta, EncodingType::scale,
				EncodingType::afl, EncodingType::none
	};
	auto tree = PathGenerator().GenerateTree(path, DataType::d_int);
	for(int i = 0; i < N; i++)
	{
		auto data = GetFakeDataWithPatternB<int>(i, 3*SIZE, -1e4, 1e4, SIZE/2);
		CompressDecompressTest<int>(tree, data);
	}
}

} /* namespace ddj */

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_licenses: MIT Unlicense


text:
#include <boost/date_time/gregorian/gregorian.hpp>
#include <string>
#include <vector>
#include <locale>
#include <iostream>

using namespace boost::gregorian;

int main()
{
  std::locale::global(std::locale{"German"});
  std::string months[12]{"Januar", "Februar", "M\xe4rz", "April",
    "Mai", "Juni", "Juli", "August", "September", "Oktober",
    "November", "Dezember"};
  std::string weekdays[7]{"Sonntag", "Montag", "Dienstag",
    "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
  date d{2014, 5, 12};
  date_facet *df = new date_facet{"%A, %d. %B %Y"};
  df->long_month_names(std::vector<std::string>{months, months + 12});
  df->long_weekday_names(std::vector<std::string>{weekdays,
    weekdays + 7});
  std::cout.imbue(std::locale{std::cout.getloc(), df});
  std::cout << d << '\n';
}

ext: cpp

max_stars_repo_name: KwangjoJeong/Boost

max_issues_repo_name: KwangjoJeong/Boost

max_forks_repo_name: KwangjoJeong/Boost


text:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <istream>
#include <iostream>
#include <ostream>
#include <vector>

#include "icmp_header.hpp"
#include "ipv4_header.hpp"

using boost::asio::ip::icmp;
using boost::asio::deadline_timer;
namespace posix_time = boost::posix_time;

class TimeSynchronizer
{
public:
  typedef std::vector<char> DataVector;
  typedef unsigned char uchar_t;
  const size_t cIntSizeInBytes = 4;
  const char cByte = 0xFF;

  TimeSynchronizer(boost::asio::io_service& ioService, const char* destination)
    : mResolver(ioService), mSocket(ioService, icmp::v4()),
      mTimer(ioService), mSequenceNumber(0), mReplyNumber(0) {
    icmp::resolver::query query(icmp::v4(), destination, "");
    mDestination = *mResolver.resolve(query);

    StartSend();
    StartReceive();
  }

private:
  void printMillisecondsAsTime(uint32_t millis) {
    posix_time::time_duration millisTimeOfDay = posix_time::milliseconds(millis);
    std::cout << millisTimeOfDay.hours() << "h " << millisTimeOfDay.minutes() << "m " << millisTimeOfDay.seconds() << "s " << "UTC" << std::endl;
  }

  //Gets milliseconds after midnight UTC
  uint32_t now() {
    auto now = posix_time::microsec_clock::universal_time().
      time_of_day();
    return static_cast<uint32_t>(now.total_milliseconds());
  }

  void addTimestamp(DataVector& buffer, uint32_t time) {
    time = htonl(time);
    for (size_t i = 0; i != cIntSizeInBytes; ++i) {
      buffer.push_back(((char*) &time)[i]);
    }
  }

  uint32_t dataVectorToTimestamp(DataVector& buffer) {
    uint32_t resultTime = 0;
    for (size_t i = 0; i != cIntSizeInBytes; ++i) {
      ((char*)(&resultTime))[i] = buffer[i];
    }
    return ntohl(resultTime);
  }

  void dumpDataVector(DataVector& vector, std::ostream& out) {
    for (auto it = vector.begin(); it != vector.end(); ++it) {
      out << *it;
    }
  }

  void readIntoDataVector(DataVector& vector, std::istream& in) {
    char ch;
    for (size_t i = 0; i < cIntSizeInBytes; ++i) {
      in.get(ch);
      vector.push_back(ch);
    }
  }

  void StartSend() {
    icmp_header timestampRequest;
    timestampRequest.type(icmp_header::timestamp_request);
    timestampRequest.code(0);
    timestampRequest.identifier(GetIdentifier());
    timestampRequest.sequence_number(++mSequenceNumber);

    // Encode the request packet.
    boost::asio::streambuf requestBuffer;
    std::ostream os(&requestBuffer);
    DataVector body;
    addTimestamp(body, now());
    addTimestamp(body, now());
    addTimestamp(body, now());
    compute_checksum(timestampRequest, body.begin(), body.end());
    os << timestampRequest;
    dumpDataVector(body, os);

    std::cout << "Old time: ";
    printMillisecondsAsTime(now());

    // Send the request.
    mTimeSent = posix_time::microsec_clock::universal_time();
    mSocket.send_to(requestBuffer.data(), mDestination);

    // Wait up to five seconds for a reply.
    mReplyNumber = 0;
    mTimer.expires_at(mTimeSent + posix_time::seconds(5));
    mTimer.async_wait(boost::bind(&TimeSynchronizer::HandleTimeout, this));
  }

  void HandleTimeout() {
    if (mReplyNumber == 0)
      std::cout << "Request timed out" << std::endl;

    // Requests must be sent no less than one second apart.
    mTimer.expires_at(mTimeSent + posix_time::seconds(1));
    mTimer.async_wait(boost::bind(&TimeSynchronizer::StartSend, this));
  }

  void StartReceive() {
    // Discard any data already in the buffer.
    mReplyBuffer.consume(mReplyBuffer.size());

    // Wait for a reply. We prepare the buffer to receive up to 64KB.
    mSocket.async_receive(mReplyBuffer.prepare(65536),
        boost::bind(&TimeSynchronizer::HandleReceive, this, _2));
  }

  void HandleReceive(std::size_t length) {
    // The actual number of bytes received is committed to the buffer so that we
    // can extract it using a std::istream object.
    mReplyBuffer.commit(length);

    // Decode the reply packet.
    std::istream is(&mReplyBuffer);
    ipv4_header ipv4Header;
    icmp_header icmpHeader;
    is >> ipv4Header >> icmpHeader;

    if (is &&
        icmpHeader.type() == icmp_header::timestamp_reply &&
        icmpHeader.identifier() == GetIdentifier() &&
        icmpHeader.sequence_number() == mSequenceNumber) {
      if (mReplyNumber++ == 0)
        mTimer.cancel();

      DataVector originate, receive, transmit;
      readIntoDataVector(originate, is);
      readIntoDataVector(receive, is);
      readIntoDataVector(transmit, is);
      uint32_t orig, recv, trans;
      orig = dataVectorToTimestamp(originate);
      recv = dataVectorToTimestamp(receive);
      trans = dataVectorToTimestamp(transmit);
      uint32_t travelTime = ((now() - trans) + (recv - orig)) / 2;
      uint32_t receivedNowTime = trans + travelTime;
      posix_time::ptime myNow = posix_time::microsec_clock::universal_time();
      std::cout
        << "Time between request and response: " << (myNow - mTimeSent).total_milliseconds() << " ms" << std::endl << "Delta: " << (receivedNowTime - now()) << " ms";
      throw std::exception();
    } else {
      StartReceive();
    }
  }

  static unsigned short GetIdentifier()
  {
#if defined(BOOST_WINDOWS)
    return static_cast<unsigned short>(::GetCurrentProcessId());
#else
    return static_cast<unsigned short>(::getpid());
#endif
  }

  icmp::resolver mResolver;
  icmp::endpoint mDestination;
  icmp::socket mSocket;
  deadline_timer mTimer;
  unsigned short mSequenceNumber;
  posix_time::ptime mTimeSent;
  boost::asio::streambuf mReplyBuffer;
  std::size_t mReplyNumber;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: timestamp <host>" << std::endl;
#if !defined(BOOST_WINDOWS)
      std::cerr << "(You may need to run this program as root.)" << std::endl;
#endif
      return 1;
    }

    boost::asio::io_service ioService;
    TimeSynchronizer p(ioService, argv[1]);
    ioService.run();
  }
  catch (std::exception& e) {
    // Just stop, quick and dirty
    // Exception driven, I know
  }
}

max_stars_repo_name: nkartashov/icmp-timestamp

max_issues_repo_name: nkartashov/icmp-timestamp

max_forks_repo_name: nkartashov/icmp-timestamp


text:
namespace w3c_sw {
    namespace TSVmanifest {

#include <boost/bind.hpp>
#include <iostream>
#include <fstream>

std::string errorStr (std::string filename, int lineNo) { // cater to emacs's comint mode
    return "                 from " + filename + ":" + boost::lexical_cast<std::string>(lineNo);
}

std::string unescape (std::string s, bool literal) {
    size_t len = s.length();
    char space[len+1];
    ::strncpy(space, s.c_str(), len);
    std::string stripped;
    if (literal)
	YaccDriver::unescapeString(space, len, &stripped, NULL);
    else
	YaccDriver::unescapeNumeric(space, len, &stripped, NULL, NULL);
    return stripped;
}

struct GetTTerm {
    const TTerm* t;
    GetTTerm (std::istream& iss, TTerm::String2BNode* bnodeMap)
	: t(readTerm(iss, bnodeMap))
    {  }
    static const TTerm* readTerm (std::istream& iss, TTerm::String2BNode* bnodeMap) {
	std::ostringstream lex;
	char ch;
	iss >> std::ws >> ch >> std::noskipws;
	switch (ch) {
	case '<':
	    while (iss >> ch && ch != '>')
		lex << ch;
	    return F.getURI(unescape(lex.str(), false));
	case '_':
	    iss >> ch; assert(ch == ':');
	    while (iss >> ch && ch != ' ' && ch != '.')
		lex << ch;
	    return F.getBNode(unescape(lex.str(), false), bnodeMap);
	case '"':
	    while (iss >> ch && ch != '"')
		if (ch == '\\') {
		    lex << ch;
		    iss >> ch;
		    lex << ch; // get past possible '"' or into u0022, or whatever
		} else {
		    lex << ch;
		}
	    {
		const TTerm* datatype = NULL;
		const LANGTAG* langtag = NULL;
		iss >> ch;
		if (ch == '^') {
		    iss >> ch; assert(ch == '^');
		    datatype = readTerm(iss, bnodeMap);
		} else if (ch == '@') {
		    std::string s;
		    iss >> s;
		    langtag = new LANGTAG(s);
		} else if (ch != ' ' && ch != '.')
		    throw std::runtime_error(std::string() + "unexpected character after string: " + ch);
		return F.getRDFLiteral(unescape(lex.str(), true), dynamic_cast<const URI*>(datatype), langtag, false);
	    }
	case '.':
	    return DefaultGraph;
	default:
	    throw std::runtime_error(std::string() + "unexpected delimiter: " + ch);
	}
    }
};

void parseNQuads (RdfDB* db, std::istream& is, std::string filename) {
    TTerm::String2BNode m;
    std::string line;
    int lineNo = 1;
    try {
	while (std::getline(is, line)) {
	    while (isspace(line[0]))
		line.erase(0, 1);
	    if (line.length() == 0 || line.find("#", 0) == 0)
		continue;
	    std::istringstream iss(line);
	    GetTTerm s(iss,&m), p(iss,&m), o(iss,&m), g(iss,&m);
	    BasicGraphPattern* graph = db->ensureGraph(g.t);
	    graph->addTriplePattern(F.getTriple(s.t, p.t, o.t));
	    ++lineNo;
	}
    } catch (std::runtime_error& e) {
	throw std::runtime_error
	    (errorStr("tests/"+filename, lineNo) + std::string(": ")+e.what());
    }
}

typedef enum {Expect, Parse, Fail} Action;
void parseAndCompare (std::string /* name */, std::string testData, std::string nqad,
		      std::string manifestLoc, Action action) {
    boost::unit_test::test_case const& cur = boost::unit_test::framework::current_test_case();
    // std::cerr << cur.p_name << "\n";
    std::string context = errorStr("tests/"+testData, 1) + "\n";
    try {
	AtomFactory::validate = AtomFactory::VALIDATE_all;
	std::string base = std::string("http://www.w3.org/2013/") + testData.c_str();
	ReferenceDB tested(testData.c_str(), base.c_str(), true);
	if (action == Expect) {
	    context = errorStr("tests/"+nqad, 1) + "\n";
	    RdfDB expect;
	    std::ifstream nqstr(nqad.c_str());
	    parseNQuads(&expect, nqstr, nqad);
	    context
		= errorStr("tests/"+testData, 1) + "\n"
		+ errorStr("tests/"+nqad, 1) + "\n";
	    if (!(tested == expect)) {
		std::stringstream l; l << tested;
		std::stringstream r; r << expect;
		BOOST_ERROR ( "\n" + context + manifestLoc + " check tested == expect failed " +
			      "[" + l.str() + " != " + r.str() + "]" );
	    }
	} else if (action == Fail) {
	    BOOST_ERROR ( "\n" + context + manifestLoc + " expected parser to fail." );
	}
	/*G_EARL.report(TEST, tested == expect);*/
    } catch (NotImplemented& e) {
	std::cerr << e.what() << "\n";
	BOOST_ERROR ( "\n" + context + manifestLoc + " " + std::string("require implementation of ") + e.brief );
    } catch (std::string& s) {
	if (action != Fail)
	    BOOST_ERROR ( "\n" + context + manifestLoc + " " + s );
    } catch (std::exception& s) {
	if (action != Fail)
	    BOOST_ERROR ( "\n" + context + manifestLoc + " " + s.what() );
    }
}

void readAndQueue (std::string filename, Action action, boost::unit_test::test_suite* ts) {
    std::ifstream tsv(filename.c_str());
    std::string name, testData, nqad;
    int lineNo = 1;
    while (tsv >> name >> testData) {
	name = name.substr(1,name.length()-2);
	testData = testData.substr(1,testData.length()-2);
	if (action == Expect) {
	    tsv >> nqad;
	    nqad = nqad.substr(1,nqad.length()-2);
	}
	ts->add(boost::unit_test::make_test_case
		(boost::unit_test::callback0<>(boost::bind
					       (&parseAndCompare, name, testData, nqad,
						errorStr("tests/"+filename, lineNo)+":",
						action)),
		 boost::unit_test::const_string(name.c_str(), name.length())));
	++lineNo;
    }
}

    } // namespace TSVmanifest
} // namespace w3c_sw

ext: hpp

max_stars_repo_name: ericprud/SWObjects

max_issues_repo_name: ericprud/SWObjects

max_forks_repo_name: ericprud/SWObjects


text:
#ifndef Module_HH
#define Module_HH

#include <armadillo>
#include "Vehicle.hh"
#include "matrix/utility.hh"

class Sensor : public FH_module {
 public:
  char name[32];
  // Preserve for output I/O to FC
};

class Actuator : public FH_module {
 public:
  char name[32];
  // Preserve for recieving input data from FC
};

class FC_Algorithm : public FH_module {
 public:
  char name[32];
  // Preserve for output I/O to FC
};

class Dynamics : public FH_module {
 public:
  char name[32];
  // Preserve for output I/O to test instrument such like SimGen,
  // Ratetable...etc
};

#endif  // Module_HH

ext: hh


text:
#ifndef TIMER_HPP_
#define TIMER_HPP_

#include <chrono>
#include <iostream>
#include <boost/format.hpp>

class Timer
{
public:
    inline Timer(const std::string &name);
    inline double elapsedSec();
    inline void print();

private:
    typedef std::chrono::high_resolution_clock clock_t;

    std::string _name;
    clock_t::time_point _start;

    inline std::string format(double sec);
};

inline Timer::Timer(const std::string &name) :
    _name(name),
    _start(clock_t::now())
{
}

inline double Timer::elapsedSec()
{
    auto current = clock_t::now();
    return std::chrono::duration<double>(current - _start).count();
}

inline void Timer::print()
{
    std::cout << _name << ": " << format(elapsedSec()) << std::endl;
}

// reference: http://stackoverflow.com/questions/4727006/c-logging-and-performance-tuning-library

inline std::string Timer::format(double sec)
{
    double d = sec;

    if (d < 0.00000001)
    {
        // show in ps with 4 digits
        return (boost::format("%0.4f ps") % (d * 1000000000000.0)).str();
    }
    if (d < 0.00001)
    {
        // show in ns
        return (boost::format("%0.0f ns")% (d * 1000000000.0)).str();
    }
    if (d < 0.001)
    {
        // show in us
        return (boost::format("%0.0f us") % (d * 1000000.0)).str();
    }
    if (d < 1.0)
    {
        // show in ms
        return (boost::format("%0.0f ms") % (d * 1000.0)).str();
    }
    if (d <= 60.0)
    {
        // show in seconds
        return (boost::format("%0.2f s") % d).str();
    }
    if (d < 3600.0)
    {
        // show in min:sec
        return (boost::format("%01.0f:%02.2f") % floor(d/60.0) % fmod(d,60.0)).str();
    }
    // show in h:min:sec
    return (boost::format("%01.0f:%02.0f:%02.2f") % floor(d/3600.0) % floor(fmod(d,3600.0)/60.0) % fmod(d,60.0)).str();
}

#endif // TIMER_HPP_