File: test_threadname.cpp

package info (click to toggle)
srt 1.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,804 kB
  • sloc: cpp: 52,175; ansic: 5,746; tcl: 1,183; sh: 318; python: 99; makefile: 38
file content (63 lines) | stat: -rw-r--r-- 1,547 bytes parent folder | download | duplicates (3)
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
#include <algorithm>
#include <string>

#include "gtest/gtest.h"
#include "threadname.h"

using namespace srt;

TEST(ThreadName, GetSet)
{
    std::string name("getset");
    char        buf[ThreadName::BUFSIZE * 2];

    memset(buf, 'a', sizeof(buf));
    ASSERT_EQ(ThreadName::get(buf), true);
    // ensure doesn't write out-of-range
    size_t max = ThreadName::BUFSIZE - 1;
    ASSERT_LE(strlen(buf), max);

    if (ThreadName::DUMMY_IMPL)
        return;

    ASSERT_EQ(ThreadName::set(name), true);
    memset(buf, 'a', sizeof(buf));
    ASSERT_EQ(ThreadName::get(buf), true);
    ASSERT_EQ(buf, name);
}

TEST(ThreadName, AutoReset)
{
    const std::string old_name("old");
    std::string new_name("new-name");
    if (ThreadName::DUMMY_IMPL)
    {
        // just make sure the API is correct
        ThreadName t(std::string("test"));
        return;
    }

    ASSERT_EQ(ThreadName::set(old_name), true);
    std::string name;
    ASSERT_EQ(ThreadName::get(name), true);
    ASSERT_EQ(name, old_name);

    {
        ThreadName threadName(new_name);
        ASSERT_EQ(ThreadName::get(name), true);
        ASSERT_EQ(name, new_name);
    }

    ASSERT_EQ(ThreadName::get(name), true);
    ASSERT_EQ(name, old_name);

    {
        new_name.resize(std::max<size_t>(512, ThreadName::BUFSIZE * 2), 'z');
        ThreadName threadName(new_name);
        ASSERT_EQ(ThreadName::get(name), true);
        ASSERT_EQ(new_name.compare(0, name.size(), name), 0);
    }

    ASSERT_EQ(ThreadName::get(name), true);
    ASSERT_EQ(name, old_name);
}