#ifndef CAFFE_UTIL_IO_H_
#define CAFFE_UTIL_IO_H_
-#include <unistd.h>
+#include <boost/filesystem.hpp>
#include <string>
#include "google/protobuf/message.h"
namespace caffe {
using ::google::protobuf::Message;
+using ::boost::filesystem::path;
inline void MakeTempFilename(string* temp_filename) {
temp_filename->clear();
- *temp_filename = "/tmp/caffe_test.XXXXXX";
- char* temp_filename_cstr = new char[temp_filename->size() + 1];
- // NOLINT_NEXT_LINE(runtime/printf)
- strcpy(temp_filename_cstr, temp_filename->c_str());
- int fd = mkstemp(temp_filename_cstr);
- CHECK_GE(fd, 0) << "Failed to open a temporary file at: " << *temp_filename;
- close(fd);
- *temp_filename = temp_filename_cstr;
- delete[] temp_filename_cstr;
+ const path& model = boost::filesystem::temp_directory_path()
+ /"caffe_test.%%%%%%";
+ *temp_filename = boost::filesystem::unique_path(model).string();
}
inline void MakeTempDir(string* temp_dirname) {
temp_dirname->clear();
- *temp_dirname = "/tmp/caffe_test.XXXXXX";
- char* temp_dirname_cstr = new char[temp_dirname->size() + 1];
- // NOLINT_NEXT_LINE(runtime/printf)
- strcpy(temp_dirname_cstr, temp_dirname->c_str());
- char* mkdtemp_result = mkdtemp(temp_dirname_cstr);
- CHECK(mkdtemp_result != NULL)
- << "Failed to create a temporary directory at: " << *temp_dirname;
- *temp_dirname = temp_dirname_cstr;
- delete[] temp_dirname_cstr;
+ const path& model = boost::filesystem::temp_directory_path()
+ /"caffe_test.%%%%%%";
+ const path& dir = boost::filesystem::unique_path(model).string();
+ bool directoryCreated = boost::filesystem::create_directory(dir);
+ CHECK(directoryCreated);
+ *temp_dirname = dir.string();
}
bool ReadProtoFromTextFile(const char* filename, Message* proto);
-#include <unistd.h> // for usleep
+#include <boost/thread.hpp>
#include "gtest/gtest.h"
EXPECT_FALSE(timer.running());
EXPECT_FALSE(timer.has_run_at_least_once());
timer.Start();
- usleep(300 * 1000);
+ boost::this_thread::sleep(boost::posix_time::milliseconds(300));
EXPECT_GE(timer.MilliSeconds(), 300 - kMillisecondsThreshold);
EXPECT_LE(timer.MilliSeconds(), 300 + kMillisecondsThreshold);
EXPECT_TRUE(timer.initted());
EXPECT_FALSE(timer.running());
EXPECT_FALSE(timer.has_run_at_least_once());
timer.Start();
- usleep(300 * 1000);
+ boost::this_thread::sleep(boost::posix_time::milliseconds(300));
EXPECT_GE(timer.Seconds(), 0.3 - kMillisecondsThreshold / 1000.);
EXPECT_LE(timer.Seconds(), 0.3 + kMillisecondsThreshold / 1000.);
EXPECT_TRUE(timer.initted());