Skip to content

Commit

Permalink
Failing unit test for #7516
Browse files Browse the repository at this point in the history
  • Loading branch information
Naios committed Sep 10, 2022
1 parent 8cdc6a2 commit 4052f43
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Expand Up @@ -230,6 +230,8 @@ set(FlatBuffers_Tests_SRCS
tests/util_test.cpp
tests/native_type_test_impl.h
tests/native_type_test_impl.cpp
tests/alignment_test.h
tests/alignment_test.cpp
include/flatbuffers/code_generators.h
src/code_generators.cpp
# file generate by running compiler on tests/monster_test.fbs
Expand All @@ -251,6 +253,8 @@ set(FlatBuffers_Tests_SRCS
${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
# file generate by running compiler on tests/native_inline_table_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/native_inline_table_test_generated.h
# file generate by running compiler on tests/alignment_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/alignment_test_generated.h
)

set(FlatBuffers_Tests_CPP17_SRCS
Expand Down Expand Up @@ -623,6 +627,7 @@ if(FLATBUFFERS_BUILD_TESTS)
compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare")
compile_flatbuffers_schema_to_cpp(tests/native_inline_table_test.fbs "--gen-compare")
compile_flatbuffers_schema_to_cpp(tests/alignment_test.fbs "--gen-compare")
if(NOT (MSVC AND (MSVC_VERSION LESS 1900)))
compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF.
endif()
Expand Down
31 changes: 31 additions & 0 deletions tests/alignment_test.cpp
@@ -0,0 +1,31 @@
#include "alignment_test.h"

#include "flatbuffers/flatbuffer_builder.h"
#include "alignment_test_generated.h"
#include "test_assert.h"

namespace flatbuffers {
namespace tests {

void AlignmentTest() {
FlatBufferBuilder builder;

BadAlignmentLarge large;
Offset<OuterLarge> outer_large = CreateOuterLarge(builder, &large);

BadAlignmentSmall *small;
Offset<Vector<const BadAlignmentSmall *>> small_offset =
builder.CreateUninitializedVectorOfStructs(9, &small);
(void)small; // We do not have to write data to trigger the test failure

Offset<BadAlignmentRoot> root =
CreateBadAlignmentRoot(builder, outer_large, small_offset);

builder.Finish(root);

Verifier verifier(builder.GetBufferPointer(), builder.GetSize());
TEST_ASSERT(VerifyBadAlignmentRootBuffer(verifier));
}

} // namespace tests
} // namespace flatbuffers
24 changes: 24 additions & 0 deletions tests/alignment_test.fbs
@@ -0,0 +1,24 @@
// sizeof(BadAlignmentSmall) == 12
// alignof(BadAlignmentSmall) == 4
struct BadAlignmentSmall {
var_0: uint;
var_1: uint;
var_2: uint;
}

// sizeof(BadAlignmentLarge) == 8
// alignof(BadAlignmentLarge) == 8
struct BadAlignmentLarge {
var_0: ulong;
}

table OuterLarge {
large: BadAlignmentLarge;
}

table BadAlignmentRoot {
large: OuterLarge;
small: [BadAlignmentSmall];
}

root_type BadAlignmentRoot;
12 changes: 12 additions & 0 deletions tests/alignment_test.h
@@ -0,0 +1,12 @@
#ifndef TESTS_ALIGNMENT_TEST_H
#define TESTS_ALIGNMENT_TEST_H

namespace flatbuffers {
namespace tests {

void AlignmentTest();

} // namespace tests
} // namespace flatbuffers

#endif
3 changes: 3 additions & 0 deletions tests/test.cpp
Expand Up @@ -20,6 +20,7 @@
#include <string>

#include "evolution_test.h"
#include "alignment_test.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/minireflect.h"
Expand Down Expand Up @@ -1415,6 +1416,8 @@ int FlatBufferTests(const std::string &tests_data_path) {

SizePrefixedTest();

AlignmentTest();

#ifndef FLATBUFFERS_NO_FILE_TESTS
ParseAndGenerateTextTest(tests_data_path, false);
ParseAndGenerateTextTest(tests_data_path, true);
Expand Down

0 comments on commit 4052f43

Please sign in to comment.