errno_unittest.cpp 1.23 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5
// Author: Ge,Jun (gejun@baidu.com)
// Date: Wed Jul 30 08:41:06 CST 2014

#include <gtest/gtest.h>
6
#include "butil/errno.h"
gejun's avatar
gejun committed
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

class ErrnoTest : public ::testing::Test{
protected:
    ErrnoTest(){
    };
    virtual ~ErrnoTest(){};
    virtual void SetUp() {
    };
    virtual void TearDown() {
    };
};

#define ESTOP -114
#define EBREAK -115
#define ESTH -116
#define EOK -117
#define EMYERROR -30

BAIDU_REGISTER_ERRNO(ESTOP, "the thread is stopping")
BAIDU_REGISTER_ERRNO(EBREAK, "the thread is interrupted")
BAIDU_REGISTER_ERRNO(ESTH, "something happened")
BAIDU_REGISTER_ERRNO(EOK, "OK!")
BAIDU_REGISTER_ERRNO(EMYERROR, "my error");

TEST_F(ErrnoTest, system_errno) {
    errno = EPIPE;
    ASSERT_STREQ("Broken pipe", berror());
    ASSERT_STREQ("Interrupted system call", berror(EINTR));
}

TEST_F(ErrnoTest, customized_errno) {
    ASSERT_STREQ("the thread is stopping", berror(ESTOP));
    ASSERT_STREQ("the thread is interrupted", berror(EBREAK));
    ASSERT_STREQ("something happened", berror(ESTH));
    ASSERT_STREQ("OK!", berror(EOK));
gejun's avatar
gejun committed
42
    ASSERT_STREQ("Unknown error 1000", berror(1000));
gejun's avatar
gejun committed
43 44 45 46 47
    
    errno = ESTOP;
    printf("Something got wrong, %m\n");
    printf("Something got wrong, %s\n", berror());
}