123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627 |
- #include <library/cpp/testing/unittest/registar.h>
- #include <library/cpp/skiff/skiff.h>
- #include <library/cpp/skiff/skiff_schema.h>
- #include <util/stream/buffer.h>
- #include <util/string/hex.h>
- using namespace NSkiff;
- ////////////////////////////////////////////////////////////////////////////////
- static TString HexEncode(const TBuffer& buffer)
- {
- auto result = HexEncode(buffer.Data(), buffer.Size());
- result.to_lower();
- return result;
- }
- Y_UNIT_TEST_SUITE(Skiff)
- {
- Y_UNIT_TEST(TestInt8)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Int8);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteInt8(42);
- tokenWriter.WriteInt8(-42);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "2a"
- "d6");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt8(), 42);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt8(), -42);
- }
- Y_UNIT_TEST(TestInt16)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Int16);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteInt16(0x1234);
- tokenWriter.WriteInt16(-0x1234);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "3412"
- "cced");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt16(), 0x1234);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt16(), -0x1234);
- }
- Y_UNIT_TEST(TestInt32)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Int32);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteInt32(0x12345678);
- tokenWriter.WriteInt32(-0x12345678);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "78563412"
- "88a9cbed");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt32(), 0x12345678);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt32(), -0x12345678);
- }
- Y_UNIT_TEST(TestInt64)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Int64);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteInt64(-42);
- tokenWriter.WriteInt64(100500);
- tokenWriter.WriteInt64(-0x123456789abcdef0);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "d6ffffffffffffff"
- "9488010000000000"
- "1021436587a9cbed");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), -42);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), 100500);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), -0x123456789abcdef0);
- }
- Y_UNIT_TEST(TestUint8)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Uint8);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteUint8(42);
- tokenWriter.WriteUint8(200);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "2a"
- "c8");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint8(), 42);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint8(), 200);
- }
- Y_UNIT_TEST(TestUint16)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Uint16);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteUint16(0x1234);
- tokenWriter.WriteUint16(0xfedc);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "3412"
- "dcfe");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint16(), 0x1234);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint16(), 0xfedc);
- }
- Y_UNIT_TEST(TestUint32)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Uint32);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteUint32(0x12345678);
- tokenWriter.WriteUint32(0x87654321);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "78563412"
- "21436587");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint32(), 0x12345678);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint32(), 0x87654321);
- }
- Y_UNIT_TEST(TestUint64)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Uint64);
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteUint64(42);
- tokenWriter.WriteUint64(100500);
- tokenWriter.WriteUint64(0x123456789abcdef0);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "2a00000000000000"
- "9488010000000000"
- "f0debc9a78563412");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 42);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 100500);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 0x123456789abcdef0);
- }
- Y_UNIT_TEST(TestInt128)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Int128);
- const TInt128 val1 = {0x1924cd4aeb9ced82, 0x0885e83f456d6a7e};
- const TInt128 val2 = {0xe9ba36585eccae1a, -0x7854b6f9ce448be9};
- TCheckedSkiffWriter writer(schema, &bufferStream);
- writer.WriteInt128(val1);
- writer.WriteInt128(val2);
- writer.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "82ed9ceb4acd2419" "7e6a6d453fe88508"
- "1aaecc5e5836bae9" "1774bb310649ab87");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_EQUAL(parser.ParseInt128(), val1);
- UNIT_ASSERT_EQUAL(parser.ParseInt128(), val2);
- }
- Y_UNIT_TEST(TestUint128)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Uint128);
- const auto val1 = TUint128{0x1924cd4aeb9ced82, 0x0885e83f456d6a7e};
- const auto val2 = TUint128{0xe9ba36585eccae1a, 0x8854b6f9ce448be9};
- TCheckedSkiffWriter writer(schema, &bufferStream);
- writer.WriteUint128(val1);
- writer.WriteUint128(val2);
- writer.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "82ed9ceb4acd2419" "7e6a6d453fe88508"
- "1aaecc5e5836bae9" "e98b44cef9b65488");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_EQUAL(parser.ParseUint128(), val1);
- UNIT_ASSERT_EQUAL(parser.ParseUint128(), val2);
- }
- Y_UNIT_TEST(TestBoolean)
- {
- auto schema = CreateSimpleTypeSchema(EWireType::Boolean);
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteBoolean(true);
- tokenWriter.WriteBoolean(false);
- tokenWriter.Finish();
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseBoolean(), true);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseBoolean(), false);
- {
- TBufferStream bufferStream;
- bufferStream.Write('\x02');
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_EXCEPTION(parser.ParseBoolean(), std::exception);
- }
- }
- Y_UNIT_TEST(TestVariant8)
- {
- auto schema = CreateVariant8Schema({
- CreateSimpleTypeSchema(EWireType::Nothing),
- CreateSimpleTypeSchema(EWireType::Uint64),
- });
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- UNIT_ASSERT_EXCEPTION(tokenWriter.WriteUint64(42), std::exception);
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant8Tag(0);
- UNIT_ASSERT_EXCEPTION(tokenWriter.WriteUint64(42), std::exception);
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant8Tag(1);
- UNIT_ASSERT_EXCEPTION(tokenWriter.WriteInt64(42), std::exception);
- }
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant8Tag(0);
- tokenWriter.WriteVariant8Tag(1);
- tokenWriter.WriteUint64(42);
- tokenWriter.Finish();
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), 0);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), 1);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 42);
- parser.ValidateFinished();
- }
- Y_UNIT_TEST(TestTuple)
- {
- auto schema = CreateTupleSchema({
- CreateSimpleTypeSchema(EWireType::Int64),
- CreateSimpleTypeSchema(EWireType::String32),
- });
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteInt64(42);
- tokenWriter.WriteString32("foobar");
- tokenWriter.Finish();
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), 42);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseString32(), "foobar");
- parser.ValidateFinished();
- }
- }
- Y_UNIT_TEST(TestString)
- {
- auto schema = CreateSimpleTypeSchema(EWireType::String32);
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteString32("foo");
- tokenWriter.Finish();
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseString32(), "foo");
- parser.ValidateFinished();
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteString32("foo");
- tokenWriter.Finish();
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_EXCEPTION(parser.ParseInt64(), std::exception);
- }
- }
- Y_UNIT_TEST(TestRepeatedVariant8)
- {
- auto schema = CreateRepeatedVariant8Schema({
- CreateSimpleTypeSchema(EWireType::Int64),
- CreateSimpleTypeSchema(EWireType::Uint64),
- });
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- // row 0
- tokenWriter.WriteVariant8Tag(0);
- tokenWriter.WriteInt64(-8);
- // row 2
- tokenWriter.WriteVariant8Tag(1);
- tokenWriter.WriteUint64(42);
- // end
- tokenWriter.WriteVariant8Tag(EndOfSequenceTag<ui8>());
- tokenWriter.Finish();
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- // row 1
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), 0);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), -8);
- // row 2
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), 1);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 42);
- // end
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), EndOfSequenceTag<ui8>());
- parser.ValidateFinished();
- }
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- UNIT_ASSERT_EXCEPTION(parser.ParseInt64(), std::exception);
- }
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- parser.ParseVariant8Tag();
- UNIT_ASSERT_EXCEPTION(parser.ParseUint64(), std::exception);
- }
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- parser.ParseVariant8Tag();
- parser.ParseInt64();
- UNIT_ASSERT_EXCEPTION(parser.ValidateFinished(), std::exception);
- }
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant8Tag(0);
- UNIT_ASSERT_EXCEPTION(tokenWriter.WriteUint64(5), std::exception);
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant8Tag(1);
- tokenWriter.WriteUint64(5);
- UNIT_ASSERT_EXCEPTION(tokenWriter.Finish(), std::exception);
- }
- }
- Y_UNIT_TEST(TestRepeatedVariant16)
- {
- auto schema = CreateRepeatedVariant16Schema({
- CreateSimpleTypeSchema(EWireType::Int64),
- CreateSimpleTypeSchema(EWireType::Uint64),
- });
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- // row 0
- tokenWriter.WriteVariant16Tag(0);
- tokenWriter.WriteInt64(-8);
- // row 2
- tokenWriter.WriteVariant16Tag(1);
- tokenWriter.WriteUint64(42);
- // end
- tokenWriter.WriteVariant16Tag(EndOfSequenceTag<ui16>());
- tokenWriter.Finish();
- TCheckedSkiffParser parser(schema, &bufferStream);
- // row 1
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), 0);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), -8);
- // row 2
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), 1);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 42);
- // end
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), EndOfSequenceTag<ui16>());
- parser.ValidateFinished();
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant16Tag(0);
- UNIT_ASSERT_EXCEPTION(tokenWriter.WriteUint64(5), std::exception);
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- tokenWriter.WriteVariant16Tag(1);
- tokenWriter.WriteUint64(5);
- UNIT_ASSERT_EXCEPTION(tokenWriter.Finish(), std::exception);
- }
- {
- TBufferStream bufferStream;
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- // row 0
- tokenWriter.WriteVariant16Tag(0);
- tokenWriter.WriteInt64(-8);
- // row 2
- tokenWriter.WriteVariant16Tag(1);
- tokenWriter.WriteUint64(42);
- // end
- tokenWriter.WriteVariant16Tag(EndOfSequenceTag<ui16>());
- tokenWriter.Finish();
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- UNIT_ASSERT_EXCEPTION(parser.ParseInt64(), std::exception);
- }
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- parser.ParseVariant16Tag();
- UNIT_ASSERT_EXCEPTION(parser.ParseUint64(), std::exception);
- }
- {
- TBufferInput input(bufferStream.Buffer());
- TCheckedSkiffParser parser(schema, &input);
- parser.ParseVariant16Tag();
- parser.ParseInt64();
- UNIT_ASSERT_EXCEPTION(parser.ValidateFinished(), std::exception);
- }
- }
- }
- Y_UNIT_TEST(TestStruct)
- {
- TBufferStream bufferStream;
- auto schema = CreateRepeatedVariant16Schema(
- {
- CreateSimpleTypeSchema(EWireType::Nothing),
- CreateTupleSchema({
- CreateVariant8Schema({
- CreateSimpleTypeSchema(EWireType::Nothing),
- CreateSimpleTypeSchema(EWireType::Int64)
- }),
- CreateSimpleTypeSchema(EWireType::Uint64),
- })
- }
- );
- {
- TCheckedSkiffWriter tokenWriter(schema, &bufferStream);
- // row 0
- tokenWriter.WriteVariant16Tag(0);
- // row 1
- tokenWriter.WriteVariant16Tag(1);
- tokenWriter.WriteVariant8Tag(0);
- tokenWriter.WriteUint64(1);
- // row 2
- tokenWriter.WriteVariant16Tag(1);
- tokenWriter.WriteVariant8Tag(1);
- tokenWriter.WriteInt64(2);
- tokenWriter.WriteUint64(3);
- // end
- tokenWriter.WriteVariant16Tag(EndOfSequenceTag<ui16>());
- tokenWriter.Finish();
- }
- TCheckedSkiffParser parser(schema, &bufferStream);
- // row 0
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), 0);
- // row 1
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), 1);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), 0);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 1);
- // row 2
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), 1);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant8Tag(), 1);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt64(), 2);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseUint64(), 3);
- // end
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseVariant16Tag(), EndOfSequenceTag<ui16>());
- parser.ValidateFinished();
- }
- Y_UNIT_TEST(TestSimpleOutputStream)
- {
- TBufferStream bufferStream;
- auto schema = CreateSimpleTypeSchema(EWireType::Int8);
- TCheckedSkiffWriter tokenWriter(schema, static_cast<IOutputStream*>(&bufferStream));
- tokenWriter.WriteInt8(42);
- tokenWriter.WriteInt8(-42);
- tokenWriter.Finish();
- UNIT_ASSERT_VALUES_EQUAL(HexEncode(bufferStream.Buffer()),
- "2a"
- "d6");
- TCheckedSkiffParser parser(schema, &bufferStream);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt8(), 42);
- UNIT_ASSERT_VALUES_EQUAL(parser.ParseInt8(), -42);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
|