Home | History | Annotate | Download | only in lite
      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 #include "tensorflow/contrib/lite/simple_memory_arena.h"
     16 
     17 #include <gmock/gmock.h>
     18 #include <gtest/gtest.h>
     19 #include "tensorflow/contrib/lite/testing/util.h"
     20 
     21 namespace tflite {
     22 namespace {
     23 
     24 TEST(SimpleMemoryArenaTest, BasicArenaOperations) {
     25   TfLiteContext context;
     26   SimpleMemoryArena arena(64);
     27   ArenaAlloc allocs[6];
     28 
     29   arena.Allocate(&context, 32, 2047, &allocs[0]);
     30   arena.Allocate(&context, 32, 2047, &allocs[1]);
     31   arena.Allocate(&context, 32, 2047, &allocs[2]);
     32   arena.Deallocate(&context, allocs[0]);
     33   arena.Allocate(&context, 32, 1023, &allocs[3]);
     34   arena.Allocate(&context, 32, 2047, &allocs[4]);
     35   arena.Deallocate(&context, allocs[1]);
     36   arena.Allocate(&context, 32, 1023, &allocs[5]);
     37 
     38   EXPECT_EQ(allocs[0].offset, 0);
     39   EXPECT_EQ(allocs[1].offset, 2048);
     40   EXPECT_EQ(allocs[2].offset, 4096);
     41   EXPECT_EQ(allocs[3].offset, 0);
     42   EXPECT_EQ(allocs[4].offset, 6144);
     43   EXPECT_EQ(allocs[5].offset, 1024);
     44 }
     45 
     46 TEST(SimpleMemoryArenaTest, TestAfterClear) {
     47   TfLiteContext context;
     48   SimpleMemoryArena arena(64);
     49   ArenaAlloc allocs[9];
     50 
     51   arena.Allocate(&context, 32, 2047, &allocs[0]);
     52   arena.Allocate(&context, 32, 2047, &allocs[1]);
     53   arena.Allocate(&context, 32, 2047, &allocs[2]);
     54   arena.Commit(&context);
     55 
     56   EXPECT_EQ(allocs[0].offset, 0);
     57   EXPECT_EQ(allocs[1].offset, 2048);
     58   EXPECT_EQ(allocs[2].offset, 4096);
     59 
     60   arena.Clear();
     61 
     62   // Test with smaller allocs.
     63   arena.Allocate(&context, 32, 1023, &allocs[3]);
     64   arena.Allocate(&context, 32, 1023, &allocs[4]);
     65   arena.Allocate(&context, 32, 1023, &allocs[5]);
     66   arena.Commit(&context);
     67 
     68   EXPECT_EQ(allocs[3].offset, 0);
     69   EXPECT_EQ(allocs[4].offset, 1024);
     70   EXPECT_EQ(allocs[5].offset, 2048);
     71 
     72   arena.Clear();
     73 
     74   // Test larger allocs which should require a reallocation.
     75   arena.Allocate(&context, 32, 4095, &allocs[6]);
     76   arena.Allocate(&context, 32, 4095, &allocs[7]);
     77   arena.Allocate(&context, 32, 4095, &allocs[8]);
     78   arena.Commit(&context);
     79 
     80   EXPECT_EQ(allocs[6].offset, 0);
     81   EXPECT_EQ(allocs[7].offset, 4096);
     82   EXPECT_EQ(allocs[8].offset, 8192);
     83 }
     84 
     85 }  // namespace
     86 }  // namespace tflite
     87 
     88 int main(int argc, char** argv) {
     89   ::tflite::LogToStderr();
     90   ::testing::InitGoogleTest(&argc, argv);
     91   return RUN_ALL_TESTS();
     92 }
     93