#include "tile/base/handle.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "gtest/gtest.h"

namespace tile {

TEST(Handle, InvalidValues)
{
    Handle h(0), h2(-1), h3(1);
    ASSERT_FALSE(h);
    ASSERT_FALSE(h2);
    ASSERT_TRUE(h3);
    auto holder = h3.Leak();
    (void) holder;
}

TEST(Handle, ValidHandle)
{
    int fd = 0;
    {
        Handle h(open("/dev/null", O_WRONLY));
        fd = h.Get();
        ASSERT_EQ(1, write(fd, "1", 1));
        ASSERT_TRUE(h);
    }
    ASSERT_EQ(-1, write(fd, "1", 1));
}

}// namespace tile
