1// Copyright 2017 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <fbl/array.h> 6 7#include <fbl/alloc_checker.h> 8#include <fbl/algorithm.h> 9#include <unittest/unittest.h> 10 11namespace { 12 13class DestructorSignaler { 14public: 15 DestructorSignaler() : array(nullptr), result(nullptr) {} 16 ~DestructorSignaler() { 17 if (array && result) 18 *result = array->get(); 19 } 20 21 fbl::Array<DestructorSignaler>* array; 22 DestructorSignaler** result; 23}; 24 25bool destructor_test() { 26 BEGIN_TEST; 27 28 DestructorSignaler bogus; 29 DestructorSignaler* result = &bogus; 30 31 fbl::AllocChecker ac; 32 DestructorSignaler* signalers = new (&ac) DestructorSignaler[2]; 33 EXPECT_TRUE(ac.check()); 34 35 { 36 fbl::Array<DestructorSignaler> array(signalers, 2); 37 array[0].array = &array; 38 array[0].result = &result; 39 } 40 41 EXPECT_FALSE(result == &bogus); 42 EXPECT_TRUE(result == nullptr); 43 44 END_TEST; 45} 46 47} // namespace 48 49BEGIN_TEST_CASE(array_tests) 50RUN_NAMED_TEST("destructor test", destructor_test) 51END_TEST_CASE(array_tests); 52