xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/schema/flatbuffer_compatibility_test.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <fstream>
17 #include <string>
18 
19 #include <gtest/gtest.h>
20 #include "flatbuffers/flatc.h"  // from @flatbuffers
21 #include "tensorflow/core/platform/platform.h"
22 
23 #ifdef PLATFORM_GOOGLE
24 #define TFLITE_TF_PREFIX "third_party/tensorflow/"
25 #else
26 #define TFLITE_TF_PREFIX "tensorflow/"
27 #endif
28 /// Load filename `name`
LoadFileRaw(const char * name,std::string * buf)29 bool LoadFileRaw(const char *name, std::string *buf) {
30   std::ifstream fp(name, std::ios::binary);
31   if (!fp) {
32     fprintf(stderr, "Failed to read '%s'\n", name);
33     return false;
34   }
35   std::string s((std::istreambuf_iterator<char>(fp)),
36                 std::istreambuf_iterator<char>());
37   if (s.empty()) {
38     fprintf(stderr, "Read '%s' resulted in empty\n", name);
39     return false;
40   }
41   *buf = s;
42   return true;
43 }
44 
ParseFile(flatbuffers::Parser * parser,const std::string & filename,const std::string & contents)45 bool ParseFile(flatbuffers::Parser *parser, const std::string &filename,
46                const std::string &contents) {
47   std::vector<const char *> include_directories;
48   auto local_include_directory = flatbuffers::StripFileName(filename);
49   include_directories.push_back(local_include_directory.c_str());
50   include_directories.push_back(nullptr);
51   if (!parser->Parse(contents.c_str(), include_directories.data(),
52                      filename.c_str())) {
53     fprintf(stderr, "Failed to parse flatbuffer schema '%s'\n",
54             contents.c_str());
55     return false;
56   }
57   return true;
58 }
59 
60 // Checks to make sure current schema in current code does not cause an
61 // incompatibility.
TEST(SchemaTest,TestCompatibility)62 TEST(SchemaTest, TestCompatibility) {
63   // Read file contents of schemas into strings
64   // TODO(aselle): Need a reliable way to load files.
65   std::string base_contents, current_contents;
66   const char *base_filename = TFLITE_TF_PREFIX "lite/schema/schema_v3b.fbs";
67   const char *current_filename =
68       TFLITE_TF_PREFIX "lite/schema/schema.fbs";
69 
70   ASSERT_TRUE(LoadFileRaw(base_filename, &base_contents));
71   ASSERT_TRUE(LoadFileRaw(current_filename, &current_contents));
72   // Parse the schemas
73   flatbuffers::Parser base_parser, current_parser;
74   std::vector<const char *> include_directories;
75   ASSERT_TRUE(ParseFile(&base_parser, base_filename, base_contents));
76   ASSERT_TRUE(ParseFile(&current_parser, current_filename, current_contents));
77   // Check that the schemas conform and fail if they don't
78   auto err = current_parser.ConformTo(base_parser);
79   if (!err.empty()) {
80     fprintf(stderr,
81             "Schemas don't conform:\n%s\n"
82             "In other words some change you made means that new parsers can't"
83             "parse old files.\n",
84             err.c_str());
85     FAIL();
86   }
87 }
88