Skip to content
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

Upgrade cpp gen to have tests and all tl features #44

Merged
merged 18 commits into from
Jul 30, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 13 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,27 @@ qtpl:
cpp_build:
g++ -o $(GEN_PATH)/test_cpp $(GEN_PATH)/test_cpp.cpp $(GEN_PATH)/cpp/all.cpp -std=c++17 -O3 -Wno-noexcept-type -g -Wall -Wextra -Werror=return-type -Wno-unused-parameter

.PHONY: cpp_gen
cpp_gen: build
cpp_template_gen: build
@./target/bin/tlgen -language=cpp -v \
--outdir=./$(GEN_PATH)/cpp \
--outdir=./$(GEN_PATH)/$(OUTPUT_PATH) \
--basicPkgPath=$(BASIC_TL_PATH) \
./$(TLS_PATH)/cpp.tl
./$(TLS_PATH)/$(TL_FILE); \

.PHONY: cpp_gen
cpp_gen:
$(MAKE) cpp_template_gen OUTPUT_PATH=cpp TL_FILE=cpp.tl

.PHONY: cpp_gen
cpp_gen_test_data:
$(MAKE) cpp_template_gen OUTPUT_PATH=cases_cpp TL_FILE=cases.tl
$(MAKE) cpp_template_gen OUTPUT_PATH=schema_cpp TL_FILE=schema.tl

.PHONY: cpp
cpp:
$(MAKE) cpp_gen
$(MAKE) cpp_build


# target should be as close as possible to github actions used to enable merge
.PHONY: check
check: build
Expand Down
184 changes: 184 additions & 0 deletions internal/tlcodegen/test/codegen_test/casetests/bytes_read_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

package casetests

import (
"encoding/json"
"fmt"
"github.com/vkcom/tl/internal/utils"
"math/rand"
"os"

"github.com/vkcom/tl/internal/tlcodegen/test/gen/cases/factory"
"github.com/vkcom/tl/internal/tlcodegen/test/gen/cases/meta"
"github.com/vkcom/tl/pkg/basictl"

"github.com/stretchr/testify/assert"
"testing"
)

type MappingSuccessBytes struct {
// expected bytes input and output
Bytes string
}

type mappingTestSamplesBytes struct {
// TL name for type to test
TestingType string
// json values which must success
Successes []MappingSuccessBytes
}

type mappingTestBytes struct {
// testing type object
object meta.Object
// testing samples with results
samples mappingTestSamplesBytes
}

type allTestsBytes struct {
Tests map[string]mappingTestSamplesBytes
}

func runMappingTestBytes(t *testing.T, mt mappingTestBytes) {
seed := rand.Uint64()
rg := basictl.NewRandGenerator(rand.New(rand.NewSource(int64(seed))))

fmt.Println("Seed: ", seed)

for sId, success := range mt.samples.Successes {
t.Run(fmt.Sprintf("Object %d", sId), func(t *testing.T) {
mt.object.FillRandom(rg)

trueBytes := utils.ParseHexToBytes(success.Bytes)
_, readErr := mt.object.Read(trueBytes)

assert.Nil(t, readErr)
writeData, writeErr := mt.object.WriteGeneral(nil)

assert.Nil(t, writeErr)
assert.Equal(t, trueBytes, writeData)

_, readAgainErr := mt.object.Read(trueBytes)
assert.Nil(t, readAgainErr)

writeAgainData, writeAgainErr := mt.object.WriteGeneral(nil)

assert.Nil(t, writeAgainErr)
assert.Equal(t, trueBytes, writeAgainData)
})

if t.Failed() {
return
}
}
}

func TestAllTLObjectsReadJsonByRandomBytes(t *testing.T) {
const RepeatNumber = 100

seed := uint64(12501105899753422230) // rand.Uint64()

t.Logf("Seed: %d\n", seed)

buf1 := make([]byte, 0)
buf2 := make([]byte, 0)

var err error

for _, tlItem := range meta.GetAllTLItems() {
t.Run(tlItem.TLName(), func(t *testing.T) {
// TODO need to check with Grisha
if tlItem.TLName() == "cases.replace7" || tlItem.TLName() == "cases.replace7plus" || tlItem.TLName() == "cases.replace7plusplus" {
t.Skip("Skip until checkSanity will be fixed")
return
}

rnd := rand.New(rand.NewSource(int64(seed)))

var objects []meta.Object
for i := 0; i < RepeatNumber; i++ {
obj := factory.CreateObject(tlItem.TLTag())
obj.FillRandom(basictl.NewRandGenerator(rnd))
objects = append(objects, obj)
}

for i := 0; i < RepeatNumber; i++ {
buf1 = buf1[:0]
buf2 = buf2[:0]
t.Run(fmt.Sprintf("Object %d", i), func(t *testing.T) {
obj := objects[i]
buf1, err = obj.WriteGeneral(buf1)
if err != nil {
t.Logf("Seed: %d\n", seed)
t.Fatal("first serialization wasn't succeeded", err.Error())
return
}
_, err = obj.Read(buf1)
if err != nil {
t.Logf("Seed: %d\n", seed)
t.Fatal("first deserialization wasn't succeeded", err.Error())
return
}
obj1 := obj
buf2, err = obj.WriteGeneral(buf2)
if err != nil {
t.Logf("Seed: %d\n", seed)
t.Fatal("second serialization wasn't succeeded", err.Error())
return
}
_, err = obj.Read(buf2)
if err != nil {
t.Logf("Seed: %d\n", seed)
t.Fatal("second deserialization wasn't succeeded", err.Error())
return
}
obj2 := obj
assert.Equal(t, buf1, buf2, "serializations must be same")
assert.Equal(t, obj1, obj2, "translations to object must be equal")
if t.Failed() {
t.Logf("Seed: %d\n", seed)
t.Logf("Test failed on %d iteration to %s", i, tlItem.TLName())
return
}
})
}
})
}
}

func TestGeneralCasesBytes(t *testing.T) {
const PathToJsonData = "../data/test-objects-bytes.json"
data, readErr := os.ReadFile(PathToJsonData)

if readErr != nil {
t.Fatalf("testing data is not provided")
return
}

tests := allTestsBytes{map[string]mappingTestSamplesBytes{}}
err := json.Unmarshal(data, &tests)

if err != nil {
t.Fatalf("can't unmarshall test data")
return
}

for testName, testValues := range tests.Tests {
t.Run(testName, func(t *testing.T) {
testObject := factory.CreateObjectFromName(testValues.TestingType)
if testObject == nil {
t.Fatalf("No testing object for test \"%s\"", testName)
return
}
runMappingTestBytes(t, mappingTestBytes{
object: testObject,
samples: testValues,
})
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package casetests
import (
"encoding/json"
"fmt"
"github.com/vkcom/tl/internal/utils"
"math/rand"
"os"

Expand Down Expand Up @@ -179,7 +180,7 @@ func TestAllTLObjectsReadJsonByRandom(t *testing.T) {
}

func TestGeneralCases(t *testing.T) {
const PathToJsonData = "data/test-json.json"
const PathToJsonData = "../data/test-objects-json.json"
data, readErr := os.ReadFile(PathToJsonData)

if readErr != nil {
Expand All @@ -196,19 +197,54 @@ func TestGeneralCases(t *testing.T) {
}

for testName, testValues := range tests.Tests {
testObject := factory.CreateObjectFromName(testValues.TestingType)
if testValues.UseBytes {
testObject = factory_bytes.CreateObjectFromNameBytes(testValues.TestingType)
}
if testObject == nil {
t.Fatalf("No testing object for test \"%s\"", testName)
return
}
t.Run(testName, func(t *testing.T) {
testObject := factory.CreateObjectFromName(testValues.TestingType)
if testValues.UseBytes {
testObject = factory_bytes.CreateObjectFromNameBytes(testValues.TestingType)
}
if testObject == nil {
t.Fatalf("No testing object for test \"%s\"", testName)
return
}
runMappingTest(t, mappingTest{
object: testObject,
samples: testValues,
})
})
}
}

func TestJson(t *testing.T) {
const PathToJsonData = "../data/test-objects-json.json"
data, readErr := os.ReadFile(PathToJsonData)

if readErr != nil {
t.Fatalf("testing data is not provided")
return
}

tests := allTests{map[string]mappingTestSamples{}}
err := json.Unmarshal(data, &tests)

if err != nil {
t.Fatalf("can't unmarshall test data")
return
}

fmt.Println("{ \"Tests\": {")
for testName, testValues := range tests.Tests {
if testValues.UseBytes {
continue
}
fmt.Printf("\"%s\": {\"TestingType\": \"%s\", \"Succesess\":[", testName, testValues.TestingType)
for _, testValue := range testValues.Successes {
testObject := factory.CreateObjectFromName(testValues.TestingType)

_ = testObject.ReadJSON(true, &basictl.JsonLexer{Data: []byte(testValue.GoldenInput)})
bs, _ := testObject.WriteGeneral(nil)
fmt.Printf("{\"Bytes\": \"%s\"},\n", utils.SprintHexDump(bs))
}
fmt.Println("]},")
}
fmt.Println("}}")
}
Loading
Loading