Repository: google/flatbuffers
File: tests/test.cpp (C++)

Code before:
  TEST_EQ(true, Parser().Parse("table T{ nan: string; }"));
  TEST_EQ(true, Parser().Parse("table T{ inf: string; }"));
#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
  TEST_EQ(true, Parser().Parse("table T{ inf: float = inf; }"));
  TEST_EQ(true, Parser().Parse("table T{ nan: float = inf; }"));
#endif
}

void NestedVerifierTest() {
  // Create a nested monster.
  flatbuffers::FlatBufferBuilder nested_builder;
  MonsterT nested_monster;
  FinishMonsterBuffer(nested_builder,
                      CreateMonster(nested_builder, &nested_monster));

  // Copy the complete nested monster to a vector
  const std::vector<uint8_t> nested_monster_bytes(
      nested_builder.GetBufferPointer(),
      nested_builder.GetBufferPointer() + nested_builder.GetSize());

  // Create the root monster.
  flatbuffers::FlatBufferBuilder builder;
  MonsterT monster;

  // Set the nested monster bytes.
  monster.testnestedflatbuffer = std::move(nested_monster_bytes);

  // Finish the root monster.
  FinishMonsterBuffer(builder, CreateMonster(builder, &monster));

  // Verify the root monster, which includes verifing the nested monster
  flatbuffers::Verifier verifier(builder.GetBufferPointer(), builder.GetSize());
  TEST_EQ(true, VerifyMonsterBuffer(verifier));

  {
    // Purposely invalidate the nested flatbuffer setting its length to 1, an
    // invalid length.
    monster.testnestedflatbuffer.resize(1);

    // Refinsih the root monster.
    FinishMonsterBuffer(builder, CreateMonster(builder, &monster));

    // Verify the root monster fails, since the included nested monster fails.
    flatbuffers::Verifier verifier(builder.GetBufferPointer(),
                                   builder.GetSize());
    TEST_EQ(false, VerifyMonsterBuffer(verifier));
  }
}

void ParseIncorrectMonsterJsonTest() {
  std::string schemafile;

Review comment (performance):
I guess this is nice to test the Object API, but this really makes 2 (!) unnecessary copies (first the nested_monster_bytes above, then the copy from the MonsterT into the real flatbuffer.. if you hadn't used `std::move` here it would be 3 copies).
That shouldn't matter for a test, but really it is good to use "idiomatic" (which for flatbuffers means "efficient") code where possible. Not using the object API, you could serialize the bytes pointer to by `GetBufferPointer` straight into the parent buffer without extra copies.

Code after:
  TEST_EQ(true, Parser().Parse("table T{ nan: string; }"));
  TEST_EQ(true, Parser().Parse("table T{ inf: string; }"));
#if defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0)
  TEST_EQ(true, Parser().Parse("table T{ inf: float = inf; }"));
  TEST_EQ(true, Parser().Parse("table T{ nan: float = inf; }"));
#endif
}

void NestedVerifierTest() {
  // Create a nested monster.
  flatbuffers::FlatBufferBuilder nested_builder;
  FinishMonsterBuffer(
      nested_builder,
      CreateMonster(nested_builder, nullptr, 0, 0,
                    nested_builder.CreateString("NestedMonster")));

  // Verify the nested monster
  flatbuffers::Verifier verifier(nested_builder.GetBufferPointer(),
                                 nested_builder.GetSize());
  TEST_EQ(true, VerifyMonsterBuffer(verifier));

  {
    // Create the outer monster.
    flatbuffers::FlatBufferBuilder builder;

    // Add the nested monster as a vector of bytes.
    auto nested_monster_bytes = builder.CreateVector(
        nested_builder.GetBufferPointer(), nested_builder.GetSize());

    auto name = builder.CreateString("OuterMonster");

    MonsterBuilder mon_builder(builder);
    mon_builder.add_name(name);
    mon_builder.add_testnestedflatbuffer(nested_monster_bytes);
    FinishMonsterBuffer(builder, mon_builder.Finish());

    // Verify the root monster, which includes verifing the nested monster
    flatbuffers::Verifier verifier(builder.GetBufferPointer(),
                                   builder.GetSize());
    TEST_EQ(true, VerifyMonsterBuffer(verifier));
  }

  {
    // Create the outer monster.
    flatbuffers::FlatBufferBuilder builder;

    // Purposely invalidate the nested flatbuffer setting its length to 1, an
    // invalid length.
    uint8_t invalid_nested_buffer[1];
    auto nested_monster_bytes = builder.CreateVector(invalid_nested_buffer, 1);