18
18
#include < executorch/runtime/core/result.h>
19
19
#include < executorch/runtime/executor/program.h>
20
20
#include < executorch/runtime/platform/runtime.h>
21
+ #include < executorch/schema/program_generated.h>
21
22
#include < executorch/test/utils/DeathTest.h>
22
23
23
24
#include < gtest/gtest.h>
@@ -89,6 +90,11 @@ class ProgramTestFriend final {
89
90
size_t index) {
90
91
return program->LoadSegment (index);
91
92
}
93
+
94
+ const static executorch_flatbuffer::Program* GetInternalProgram (
95
+ const Program* program) {
96
+ return program->internal_program_ ;
97
+ }
92
98
};
93
99
} // namespace testing
94
100
} // namespace executor
@@ -299,9 +305,6 @@ TEST_F(ProgramTest, HeaderNotPresent) {
299
305
Program::HeaderStatus::NotPresent);
300
306
}
301
307
302
- // TODO(T144120904): Add tests for programs with segments once we can generate
303
- // them.
304
-
305
308
TEST_F (ProgramTest, getMethods) {
306
309
// Parse the Program from the data.
307
310
Result<Program> program_res =
@@ -326,3 +329,70 @@ TEST_F(ProgramTest, DEPRECATEDLoad) {
326
329
Result<Program> program_res = Program::Load (multi_loader_.get ());
327
330
EXPECT_EQ (program_res.error (), Error::Ok);
328
331
}
332
+
333
+ TEST_F (ProgramTest, LoadConstantSegment) {
334
+ // Load the serialized ModuleLinear data, with constants in the segment and no
335
+ // constants in the flatbuffer.
336
+ const char * linear_path =
337
+ std::getenv (" ET_MODULE_LINEAR_CONSTANT_SEGMENT_PATH" );
338
+ Result<FileDataLoader> linear_loader = FileDataLoader::from (linear_path);
339
+ ASSERT_EQ (linear_loader.error (), Error::Ok);
340
+
341
+ // This file should always be compatible.
342
+ Result<FreeableBuffer> linear_header =
343
+ linear_loader->Load (/* offset=*/ 0 , Program::kMinHeadBytes );
344
+ ASSERT_EQ (linear_header.error (), Error::Ok);
345
+ EXPECT_EQ (
346
+ Program::check_header (linear_header->data (), linear_header->size ()),
347
+ Program::HeaderStatus::CompatibleVersion);
348
+
349
+ Result<Program> program = Program::load (&linear_loader.get ());
350
+ ASSERT_EQ (program.error (), Error::Ok);
351
+
352
+ // Load constant segment data.
353
+ Result<FreeableBuffer> segment =
354
+ ProgramTestFriend::LoadSegment (&program.get (), 0 );
355
+ EXPECT_EQ (segment.error (), Error::Ok);
356
+
357
+ const executorch_flatbuffer::Program* flatbuffer_program =
358
+ ProgramTestFriend::GetInternalProgram (&program.get ());
359
+
360
+ // Expect one segment containing the constants.
361
+ EXPECT_EQ (flatbuffer_program->segments ()->size (), 1 );
362
+
363
+ // The constant buffer should be empty.
364
+ EXPECT_EQ (flatbuffer_program->constant_buffer ()->size (), 0 );
365
+
366
+ // Check constant segment offsets.
367
+ EXPECT_EQ (flatbuffer_program->constant_segment ()->segment_index (), 0 );
368
+ EXPECT_GE (flatbuffer_program->constant_segment ()->offsets ()->size (), 1 );
369
+ }
370
+
371
+ TEST_F (ProgramTest, LoadConstantSegmentWithNoConstantSegment) {
372
+ // Load the serialized ModuleLinear data, with constants in the flatbuffer and
373
+ // no constants in the segment.
374
+ const char * linear_path =
375
+ std::getenv (" ET_MODULE_LINEAR_CONSTANT_BUFFER_PATH" );
376
+ Result<FileDataLoader> linear_loader = FileDataLoader::from (linear_path);
377
+ ASSERT_EQ (linear_loader.error (), Error::Ok);
378
+
379
+ // This file should always be compatible.
380
+ Result<FreeableBuffer> linear_header =
381
+ linear_loader->Load (/* offset=*/ 0 , Program::kMinHeadBytes );
382
+ ASSERT_EQ (linear_header.error (), Error::Ok);
383
+ EXPECT_EQ (
384
+ Program::check_header (linear_header->data (), linear_header->size ()),
385
+ Program::HeaderStatus::CompatibleVersion);
386
+
387
+ Result<Program> program = Program::load (&linear_loader.get ());
388
+ ASSERT_EQ (program.error (), Error::Ok);
389
+
390
+ const executorch_flatbuffer::Program* flatbuffer_program =
391
+ ProgramTestFriend::GetInternalProgram (&program.get ());
392
+
393
+ // Expect no segments.
394
+ EXPECT_EQ (flatbuffer_program->segments ()->size (), 0 );
395
+
396
+ // The constant buffer should exist.
397
+ EXPECT_GE (flatbuffer_program->constant_buffer ()->size (), 1 );
398
+ }
0 commit comments