diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespalib/src/tests/guard |
Publish
Diffstat (limited to 'vespalib/src/tests/guard')
-rw-r--r-- | vespalib/src/tests/guard/.gitignore | 6 | ||||
-rw-r--r-- | vespalib/src/tests/guard/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/guard/DESC | 1 | ||||
-rw-r--r-- | vespalib/src/tests/guard/FILES | 1 | ||||
-rw-r--r-- | vespalib/src/tests/guard/guard_test.cpp | 268 |
5 files changed, 284 insertions, 0 deletions
diff --git a/vespalib/src/tests/guard/.gitignore b/vespalib/src/tests/guard/.gitignore new file mode 100644 index 00000000000..ef84d571208 --- /dev/null +++ b/vespalib/src/tests/guard/.gitignore @@ -0,0 +1,6 @@ +.depend +Makefile +filedesc.txt +filept.txt +guard_test +vespalib_guard_test_app diff --git a/vespalib/src/tests/guard/CMakeLists.txt b/vespalib/src/tests/guard/CMakeLists.txt new file mode 100644 index 00000000000..dc4ba9fcd64 --- /dev/null +++ b/vespalib/src/tests/guard/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_guard_test_app + SOURCES + guard_test.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_guard_test_app COMMAND vespalib_guard_test_app) diff --git a/vespalib/src/tests/guard/DESC b/vespalib/src/tests/guard/DESC new file mode 100644 index 00000000000..2c56a9e5a08 --- /dev/null +++ b/vespalib/src/tests/guard/DESC @@ -0,0 +1 @@ +guard test. Take a look at guard.cpp for details. diff --git a/vespalib/src/tests/guard/FILES b/vespalib/src/tests/guard/FILES new file mode 100644 index 00000000000..8930baccba7 --- /dev/null +++ b/vespalib/src/tests/guard/FILES @@ -0,0 +1 @@ +guard.cpp diff --git a/vespalib/src/tests/guard/guard_test.cpp b/vespalib/src/tests/guard/guard_test.cpp new file mode 100644 index 00000000000..a663edc4eec --- /dev/null +++ b/vespalib/src/tests/guard/guard_test.cpp @@ -0,0 +1,268 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/fastos/fastos.h> +#include <vespa/log/log.h> +LOG_SETUP("guard_test"); +#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/vespalib/util/guard.h> + +using namespace vespalib; + +class Test : public TestApp +{ +public: + void testFilePointer(); + void testFileDescriptor(); + void testDirPointer(); + void testValueGuard(); + void testMaxValueGuard(); + void testCounterGuard(); + int Main(); +}; + +void +Test::testFilePointer() +{ + { + FilePointer file(fopen("bogus", "r")); + EXPECT_TRUE(!file.valid()); + } + { + FilePointer file(fopen("filept.txt", "w")); + EXPECT_TRUE(file.valid()); + fprintf(file, "Hello"); + } + { + FilePointer file(fopen("filept.txt", "r")); + EXPECT_TRUE(file.valid()); + char tmp[128]; + fgets(tmp, sizeof(tmp), file); + EXPECT_TRUE(strcmp(tmp, "Hello") == 0); + } + { + FILE *pt = NULL; + { + FilePointer file(fopen("filept.txt", "r")); + EXPECT_TRUE(file.valid()); + pt = file; + } + EXPECT_TRUE(pt != NULL); + // char tmp[128]; + // EXPECT_TRUE(fgets(tmp, sizeof(tmp), pt) == NULL); + } + { + FilePointer file(fopen("filept.txt", "w")); + EXPECT_TRUE(file.valid()); + fprintf(file, "World"); + + file.reset(fopen("filept.txt", "r")); + EXPECT_TRUE(file.valid()); + char tmp[128]; + fgets(tmp, sizeof(tmp), file.fp()); + EXPECT_TRUE(strcmp(tmp, "World") == 0); + + FILE *ref = file.fp(); + FILE *fp = file.release(); + EXPECT_TRUE(fp != NULL); + EXPECT_TRUE(fp == ref); + EXPECT_TRUE(!file.valid()); + EXPECT_TRUE(file.fp() == NULL); + fclose(fp); + } +} + +void +Test::testFileDescriptor() +{ + { + FileDescriptor file(open("bogus", O_RDONLY)); + EXPECT_TRUE(!file.valid()); + } + { + FileDescriptor file(open("filedesc.txt", O_WRONLY | O_CREAT, 0644)); + EXPECT_TRUE(file.valid()); + EXPECT_TRUE((size_t)write(file.fd(), "Hello", strlen("Hello")) == strlen("Hello")); + } + { + FileDescriptor file(open("filedesc.txt", O_RDONLY)); + EXPECT_TRUE(file.valid()); + char tmp[128]; + size_t res = read(file.fd(), tmp, sizeof(tmp)); + EXPECT_TRUE(res == strlen("Hello")); + tmp[res] = '\0'; + EXPECT_TRUE(strcmp(tmp, "Hello") == 0); + } + { + int fd = -1; + { + FileDescriptor file(open("filedesc.txt", O_RDONLY)); + EXPECT_TRUE(file.valid()); + fd = file.fd(); + } + char tmp[128]; + EXPECT_TRUE(read(fd, tmp, sizeof(tmp)) == -1); + } + { + FileDescriptor file(open("filedesc.txt", O_WRONLY | O_CREAT, 0644)); + EXPECT_TRUE(file.valid()); + EXPECT_TRUE((size_t)write(file.fd(), "World", strlen("World")) == strlen("World")); + + file.reset(open("filedesc.txt", O_RDONLY)); + EXPECT_TRUE(file.valid()); + char tmp[128]; + size_t res = read(file.fd(), tmp, sizeof(tmp)); + EXPECT_TRUE(res == strlen("World")); + tmp[res] = '\0'; + EXPECT_TRUE(strcmp(tmp, "World") == 0); + + int ref = file.fd(); + int fd = file.release(); + EXPECT_TRUE(fd >= 0); + EXPECT_TRUE(fd == ref); + EXPECT_TRUE(!file.valid()); + EXPECT_TRUE(file.fd() == -1); + close(fd); + } +} + +void +Test::testDirPointer() +{ + { + DirPointer dir(opendir("bogus")); + EXPECT_TRUE(!dir.valid()); + } + { + DirPointer dir(opendir(".")); + EXPECT_TRUE(dir.valid()); + + dirent *de; + bool foundGuardCpp = false; + while ((de = readdir(dir)) != NULL) { + if (strcmp(de->d_name, "guard_test.cpp") == 0) { + foundGuardCpp = true; + } + } + EXPECT_TRUE(foundGuardCpp); + } + { + DIR *dp = NULL; + { + DirPointer dir(opendir(".")); + EXPECT_TRUE(dir.valid()); + dp = dir; + } + EXPECT_TRUE(dp != NULL); + // EXPECT_TRUE(readdir(dp) == NULL); + } + { + DirPointer dir(opendir(".")); + EXPECT_TRUE(dir.valid()); + dir.reset(opendir(".")); + EXPECT_TRUE(dir.valid()); + + DIR *ref = dir.dp(); + DIR *dp = dir.release(); + EXPECT_TRUE(dp != NULL); + EXPECT_TRUE(dp == ref); + EXPECT_TRUE(!dir.valid()); + EXPECT_TRUE(dir.dp() == NULL); + closedir(dp); + } +} + +void +Test::testValueGuard() +{ + int value = 10; + { + ValueGuard<int> guard(value); + value = 20; + EXPECT_TRUE(value == 20); + } + EXPECT_TRUE(value == 10); + { + ValueGuard<int> guard(value, 50); + value = 20; + EXPECT_TRUE(value == 20); + } + EXPECT_TRUE(value == 50); + { + ValueGuard<int> guard(value); + value = 20; + guard.update(100); + EXPECT_TRUE(value == 20); + } + EXPECT_TRUE(value == 100); + { + ValueGuard<int> guard(value); + value = 20; + guard.dismiss(); + EXPECT_TRUE(value == 20); + } + EXPECT_TRUE(value == 20); +} + +void +Test::testMaxValueGuard() +{ + int value = 10; + { + MaxValueGuard<int> guard(value); + value = 20; + EXPECT_TRUE(value == 20); + } + EXPECT_TRUE(value == 10); + { + MaxValueGuard<int> guard(value); + value = 5; + EXPECT_TRUE(value == 5); + } + EXPECT_TRUE(value == 5); + { + MaxValueGuard<int> guard(value, 50); + value = 100; + EXPECT_TRUE(value == 100); + } + EXPECT_TRUE(value == 50); + { + MaxValueGuard<int> guard(value); + value = 200; + guard.update(100); + EXPECT_TRUE(value == 200); + } + EXPECT_TRUE(value == 100); + { + MaxValueGuard<int> guard(value); + value = 200; + guard.dismiss(); + EXPECT_TRUE(value == 200); + } + EXPECT_TRUE(value == 200); +} + +void +Test::testCounterGuard() +{ + int cnt = 10; + { + EXPECT_TRUE(cnt == 10); + CounterGuard guard(cnt); + EXPECT_TRUE(cnt == 11); + } + EXPECT_TRUE(cnt == 10); +} + +int +Test::Main() +{ + TEST_INIT("guard_test"); + testFilePointer(); + testFileDescriptor(); + testDirPointer(); + testValueGuard(); + testMaxValueGuard(); + testCounterGuard(); + TEST_DONE(); +} + +TEST_APPHOOK(Test) |