test_tool.h 1.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_TEST_TOOL_H
#define BAIDU_PADDLE_SERVING_PREDICTOR_TEST_TOOL_H

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include<unistd.h>
#include <sys/time.h>
#include <gtest/gtest.h>

namespace baidu {
namespace paddle_serving {
namespace unittest {

class AutoTempFile {
public:
    AutoTempFile(const char* content) {
        _need_del = false;
        _name = generate_temp_name();
        FILE* fd = fopen(_name.c_str(), "w");
        if (!fd) {
            return ;
        }
        fprintf(fd, "%s", content);
        fclose(fd);
        _need_del = true;
    }

    ~AutoTempFile() {
        if (_need_del) {
            remove(_name.c_str());
        }
    }

    const char* name() {
        return _name.c_str();
    }

private:
    std::string generate_temp_name() {
        timeval tv;
        srand(time(0));
        gettimeofday(&tv, NULL);
        std::ostringstream oss;
        oss << "uttest_temp_";
        oss << tv.tv_sec * 1000 + tv.tv_usec / 1000;
        oss << "_";
        oss << (int)getpid();
        oss << "_";
        oss << rand();
        oss << ".conf";
        return oss.str();
    }

private:
    std::string _name;
    bool _need_del;
};

}
}
}

#endif