1// Copyright 2014 The Android Open Source Project 2// 3// This software is licensed under the terms of the GNU General Public 4// License version 2, as published by the Free Software Foundation, and 5// may be copied, distributed, and modified under those terms. 6// 7// This program is distributed in the hope that it will be useful, 8// but WITHOUT ANY WARRANTY; without even the implied warranty of 9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10// GNU General Public License for more details. 11 12#include "android/base/files/ScopedStdioFile.h" 13 14#include <gtest/gtest.h> 15 16namespace android { 17namespace base { 18 19// The path of a file that can always be opened for reading on any platform. 20#ifdef _WIN32 21static const char kNullFile[] = "NUL"; 22#else 23static const char kNullFile[] = "/dev/null"; 24#endif 25 26TEST(ScopedStdioFile, DefaultConstructor) { 27 ScopedStdioFile f; 28 EXPECT_FALSE(f.get()); 29} 30 31TEST(ScopedStdioFile, Constructor) { 32 ScopedStdioFile f(fopen(kNullFile, "rb")); 33 EXPECT_TRUE(f.get()); 34} 35 36TEST(ScopedStdioFile, Release) { 37 FILE* handle = NULL; 38 ScopedStdioFile f(fopen(kNullFile, "rb")); 39 EXPECT_TRUE(f.get()); 40 handle = f.release(); 41 EXPECT_FALSE(f.get()); 42 EXPECT_TRUE(handle); 43 ::fclose(handle); 44} 45 46TEST(ScopedStdioFile, Close) { 47 ScopedStdioFile f(fopen(kNullFile, "rb")); 48 EXPECT_TRUE(f.get()); 49 f.close(); 50 EXPECT_FALSE(f.get()); 51} 52 53TEST(ScopedStdioFile, Swap) { 54 ScopedStdioFile f1; 55 ScopedStdioFile f2(fopen(kNullFile, "rb")); 56 EXPECT_FALSE(f1.get()); 57 EXPECT_TRUE(f2.get()); 58 f1.swap(&f2); 59 EXPECT_FALSE(f2.get()); 60 EXPECT_TRUE(f1.get()); 61} 62 63} // namespace base 64} // namespace android 65