1 /* Copyright 2018 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 "tensorflow/c/env.h"
17
18 #include "tensorflow/c/c_api.h"
19 #include "tensorflow/core/lib/io/path.h"
20 #include "tensorflow/core/platform/mutex.h"
21 #include "tensorflow/core/platform/test.h"
22 #include "tensorflow/core/platform/types.h"
23
24 #define ASSERT_TF_OK(x) ASSERT_EQ(TF_OK, TF_GetCode(x))
25
TEST(TestEnv,TestDirHandling)26 TEST(TestEnv, TestDirHandling) {
27 TF_StringStream* tempdirs = TF_GetLocalTempDirectories();
28 const char* tempdir;
29 bool found = false;
30 while (TF_StringStreamNext(tempdirs, &tempdir)) {
31 found = true;
32
33 TF_Status* s = TF_NewStatus();
34
35 ::tensorflow::string dirpath =
36 ::tensorflow::io::JoinPath(tempdir, "somedir");
37 TF_CreateDir(dirpath.c_str(), s);
38 ASSERT_TF_OK(s) << "TF_CreateDir failed for " << dirpath << ": "
39 << TF_Message(s);
40
41 ::tensorflow::string filepath =
42 ::tensorflow::io::JoinPath(dirpath, "somefile.txt");
43 TF_WritableFileHandle* handle;
44 TF_NewWritableFile(filepath.c_str(), &handle, s);
45 ASSERT_TF_OK(s) << "NewWritableFile failed for " << filepath << ": "
46 << TF_Message(s);
47
48 const char* data = "Hello, world!\n";
49 TF_AppendWritableFile(handle, data, strlen(data), s);
50 ASSERT_TF_OK(s) << "TF_AppendWritableFile failed to append data to file at "
51 << filepath << ": " << TF_Message(s);
52
53 TF_CloseWritableFile(handle, s);
54 ASSERT_TF_OK(s) << "TF_CloseWritableFile failed to close handle to "
55 << filepath << ": " << TF_Message(s);
56
57 TF_StringStream* children = TF_GetChildren(dirpath.c_str(), s);
58 ASSERT_TF_OK(s) << "TF_GetChildren failed for " << dirpath;
59 const char* childpath;
60 ASSERT_TRUE(TF_StringStreamNext(children, &childpath));
61 ASSERT_EQ(::tensorflow::string(childpath), "somefile.txt");
62 // There should only be one file in this directory.
63 ASSERT_FALSE(TF_StringStreamNext(children, &childpath));
64 ASSERT_EQ(childpath, nullptr);
65 TF_StringStreamDone(children);
66
67 TF_FileStatistics stats;
68 TF_FileStat(filepath.c_str(), &stats, s);
69 ASSERT_EQ(stats.length, strlen(data));
70 ASSERT_FALSE(stats.is_directory);
71 ASSERT_GT(stats.mtime_nsec, 0);
72
73 // Trying to delete a non-empty directory should fail.
74 TF_DeleteDir(dirpath.c_str(), s);
75 ASSERT_NE(TF_OK, TF_GetCode(s))
76 << "TF_DeleteDir unexpectedly succeeded with a non-empty directory "
77 << dirpath;
78
79 TF_DeleteFile(filepath.c_str(), s);
80 ASSERT_TF_OK(s) << "TF_DeleteFile failed for " << filepath << ": "
81 << TF_Message(s);
82
83 // Now deleting the directory should work.
84 TF_DeleteDir(dirpath.c_str(), s);
85 ASSERT_TF_OK(s) << "TF_DeleteDir failed for " << dirpath << ": "
86 << TF_Message(s);
87
88 TF_DeleteStatus(s);
89 break;
90 }
91
92 ASSERT_TRUE(found) << "expected at least one temp dir";
93
94 TF_StringStreamDone(tempdirs);
95 }
96
TEST(TestEnv,TestTimeFunctions)97 TEST(TestEnv, TestTimeFunctions) {
98 ASSERT_GE(TF_NowSeconds(), 946684800); // Midnight Jan 1, 2000
99 ASSERT_GE(TF_NowMicros(), 946684800 * 1e6);
100 ASSERT_GE(TF_NowNanos(), 946684800 * 1e9);
101 }
102
103 namespace {
104
105 struct SomeThreadData {
106 ::tensorflow::mutex mu;
107 bool did_work = false;
108 };
109
SomeThreadFunc(void * data)110 void SomeThreadFunc(void* data) {
111 auto* real_data = static_cast<SomeThreadData*>(data);
112 ::tensorflow::mutex_lock l(real_data->mu);
113 real_data->did_work = true;
114 }
115
116 } // namespace
117
TEST(TestEnv,TestThreads)118 TEST(TestEnv, TestThreads) {
119 TF_ThreadOptions options;
120 TF_DefaultThreadOptions(&options);
121 SomeThreadData data;
122 TF_Thread* thread =
123 TF_StartThread(&options, "SomeThreadName", &SomeThreadFunc, &data);
124 TF_JoinThread(thread);
125 ::tensorflow::mutex_lock l(data.mu);
126 ASSERT_TRUE(data.did_work);
127 }
128