static void test_set_unset_reset () { gdb_environ env = gdb_environ::from_host_environ (); /* Our test variable should already be present in the host's environment. */ SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") != NULL); /* Set our test variable to another value. */ env.set ("GDB_SELFTEST_ENVIRON", "test"); SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "test") == 0); SELF_CHECK (env.user_set_env ().size () == 1); SELF_CHECK (env.user_unset_env ().size () == 0); /* And unset our test variable. The variable still exists in the host's environment, but doesn't exist in our vector. */ env.unset ("GDB_SELFTEST_ENVIRON"); SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON") == NULL); SELF_CHECK (env.user_set_env ().size () == 0); SELF_CHECK (env.user_unset_env ().size () == 1); SELF_CHECK (set_contains (env.user_unset_env (), std::string ("GDB_SELFTEST_ENVIRON"))); /* Re-set the test variable. */ env.set ("GDB_SELFTEST_ENVIRON", "1"); SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON"), "1") == 0); }
static void test_vector_clear () { gdb_environ env; env.set (gdb_selftest_env_var, "1"); env.clear (); SELF_CHECK (env.envp ()[0] == NULL); SELF_CHECK (env.user_set_env ().size () == 0); SELF_CHECK (env.user_unset_env ().size () == 0); SELF_CHECK (env.get (gdb_selftest_env_var) == NULL); }
static void test_vector_initialization () { gdb_environ env; /* When the vector is initialized, there should always be one NULL element in it. */ SELF_CHECK (env.envp ()[0] == NULL); SELF_CHECK (env.user_set_env ().size () == 0); SELF_CHECK (env.user_unset_env ().size () == 0); /* Make sure that there is no other element. */ SELF_CHECK (env.get ("PWD") == NULL); }
static void test_set_A_unset_B_unset_A_cannot_find_A_can_find_B () { gdb_environ env; env.set ("GDB_SELFTEST_ENVIRON_1", "aaa"); SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_1"), "aaa") == 0); /* User-set environ var list must contain one element. */ SELF_CHECK (env.user_set_env ().size () == 1); SELF_CHECK (set_contains (env.user_set_env (), std::string ("GDB_SELFTEST_ENVIRON_1=aaa"))); env.set ("GDB_SELFTEST_ENVIRON_2", "bbb"); SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0); env.unset ("GDB_SELFTEST_ENVIRON_1"); SELF_CHECK (env.get ("GDB_SELFTEST_ENVIRON_1") == NULL); SELF_CHECK (strcmp (env.get ("GDB_SELFTEST_ENVIRON_2"), "bbb") == 0); /* The user-set environ var list must contain only one element now. */ SELF_CHECK (set_contains (env.user_set_env (), std::string ("GDB_SELFTEST_ENVIRON_2=bbb"))); SELF_CHECK (env.user_set_env ().size () == 1); }
static void test_init_from_host_environ () { /* Initialize the environment vector using the host's environ. */ gdb_environ env = gdb_environ::from_host_environ (); /* The user-set and user-unset lists must be empty. */ SELF_CHECK (env.user_set_env ().size () == 0); SELF_CHECK (env.user_unset_env ().size () == 0); /* Our test environment variable should be present at the vector. */ SELF_CHECK (strcmp (env.get (gdb_selftest_env_var), "1") == 0); }
/* Test that the file is unmapped. */ static void test_destroy () { void *mem; errno = 0; { ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); mem = smmap.get (); SELF_CHECK (mem != nullptr); } SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == -1 && errno == ENOMEM); }
static void test_appendf_func (string_appendf_func *func) { std::string str; func (str, "%s", ""); SELF_CHECK (str == ""); func (str, "%s", "test"); SELF_CHECK (str == "test"); func (str, "%d", 23); SELF_CHECK (str == "test23"); func (str, "%s %d %s", "foo", 45, "bar"); SELF_CHECK (str == "test23foo 45 bar"); }
/* Test that the memory can be released. */ static void test_release () { void *mem; errno = 0; { ::scoped_mmap smmap (nullptr, sysconf (_SC_PAGESIZE), PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); mem = smmap.release (); SELF_CHECK (mem != nullptr); } SELF_CHECK (msync (mem, sysconf (_SC_PAGESIZE), 0) == 0 || errno != ENOMEM); munmap (mem, sysconf (_SC_PAGESIZE)); }
/* Test the standard usage of mmap_file. */ static void test_normal () { char filename[] = "scoped_mmapped_file-selftest-XXXXXX"; int fd = mkstemp (filename); SELF_CHECK (fd >= 0); SELF_CHECK (write (fd, "Hello!", 7) == 7); close (fd); gdb::unlinker unlink_test_file (filename); { ::scoped_mmap m = ::mmap_file (filename); SELF_CHECK (m.get () != MAP_FAILED); SELF_CHECK (m.size () == 7); SELF_CHECK (0 == strcmp ((char *) m.get (), "Hello!")); } }
static void test_unset_set_empty_vector () { gdb_environ env; env.set ("PWD", "test"); SELF_CHECK (strcmp (env.get ("PWD"), "test") == 0); SELF_CHECK (set_contains (env.user_set_env (), std::string ("PWD=test"))); SELF_CHECK (env.user_unset_env ().size () == 0); /* The second element must be NULL. */ SELF_CHECK (env.envp ()[1] == NULL); SELF_CHECK (env.user_set_env ().size () == 1); env.unset ("PWD"); SELF_CHECK (env.envp ()[0] == NULL); SELF_CHECK (env.user_set_env ().size () == 0); SELF_CHECK (env.user_unset_env ().size () == 1); SELF_CHECK (set_contains (env.user_unset_env (), std::string ("PWD"))); }
static void unpack_field_as_long_tests (struct gdbarch *arch) { gdb_byte buffer[8]; const struct builtin_type *bt = builtin_type (arch); struct type *struct_type = arch_composite_type (arch, "<<selftest>>", TYPE_CODE_STRUCT); append_composite_type_field (struct_type, "field0", bt->builtin_int8); append_composite_type_field_aligned (struct_type, "field1", bt->builtin_uint32, 4); memset (buffer, 0, sizeof (buffer)); buffer[0] = 255; if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG) buffer[7] = 23; else buffer[4] = 23; SELF_CHECK (unpack_field_as_long (struct_type, buffer, 0) == -1); SELF_CHECK (unpack_field_as_long (struct_type, buffer, 1) == 23); }
/* Calling mmap_file with a non-existent file should throw an exception. */ static void test_invalid_filename () { bool threw = false; try { ::scoped_mmap m = ::mmap_file ("/this/file/should/not/exist"); } catch (gdb_exception &e) { threw = true; } SELF_CHECK (threw); }
static void test_reinit_from_host_environ () { /* Reinitialize our environ vector using the host environ. We should be able to see one (and only one) instance of the test variable. */ gdb_environ env = gdb_environ::from_host_environ (); env = gdb_environ::from_host_environ (); char **envp = env.envp (); int num_found = 0; for (size_t i = 0; envp[i] != NULL; ++i) if (strcmp (envp[i], "GDB_SELFTEST_ENVIRON=1") == 0) ++num_found; SELF_CHECK (num_found == 1); }
static void common_utils_tests (void) { SELF_CHECK (string_printf ("%s", "") == ""); SELF_CHECK (string_printf ("%d comes before 2", 1) == "1 comes before 2"); SELF_CHECK (string_printf ("hello %s", "world") == "hello world"); #define X10 "0123456789" #define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10 #define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100 #define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 #define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 SELF_CHECK (string_printf ("%s", X10) == X10); SELF_CHECK (string_printf ("%s", X100) == X100); SELF_CHECK (string_printf ("%s", X1000) == X1000); SELF_CHECK (string_printf ("%s", X10000) == X10000); SELF_CHECK (string_printf ("%s", X100000) == X100000); }
static void test_self_move () { gdb_environ env; /* Test self-move. */ env.set ("A", "1"); SELF_CHECK (strcmp (env.get ("A"), "1") == 0); SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1"))); SELF_CHECK (env.user_set_env ().size () == 1); /* Some compilers warn about moving to self, but that's precisely what we want to test here, so turn this warning off. */ DIAGNOSTIC_PUSH DIAGNOSTIC_IGNORE_SELF_MOVE env = std::move (env); DIAGNOSTIC_POP SELF_CHECK (strcmp (env.get ("A"), "1") == 0); SELF_CHECK (strcmp (env.envp ()[0], "A=1") == 0); SELF_CHECK (env.envp ()[1] == NULL); SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1"))); SELF_CHECK (env.user_set_env ().size () == 1); }
static void aarch64_tdesc_test () { const target_desc *tdesc = aarch64_linux_read_description (0); SELF_CHECK (*tdesc == *tdesc_aarch64); }
static void test () { SELF_CHECK (child_path_check ("/one", "/two", NULL)); SELF_CHECK (child_path_check ("/one", "/one", NULL)); SELF_CHECK (child_path_check ("/one", "/one/", NULL)); SELF_CHECK (child_path_check ("/one", "/one//", NULL)); SELF_CHECK (child_path_check ("/one", "/one/two", "two")); SELF_CHECK (child_path_check ("/one/", "/two", NULL)); SELF_CHECK (child_path_check ("/one/", "/one", NULL)); SELF_CHECK (child_path_check ("/one/", "/one/", NULL)); SELF_CHECK (child_path_check ("/one/", "/one//", NULL)); SELF_CHECK (child_path_check ("/one/", "/one/two", "two")); SELF_CHECK (child_path_check ("/one/", "/one//two", "two")); SELF_CHECK (child_path_check ("/one/", "/one//two/", "two/")); SELF_CHECK (child_path_check ("/one", "/onetwo", NULL)); SELF_CHECK (child_path_check ("/one", "/onetwo/three", NULL)); }
static void run_tests () { capacity_1::main (); cons_1::main (); cons_2::main (); cons_3::main (); element_access_1::main (); element_access_empty::main (); element_access_front_back::main (); inserters_2::main (); modifiers_remove_prefix::main (); modifiers_remove_suffix::main (); modifiers_swap::test01 (); operations_compare_1::main (); operations_compare_13650::main (); operations_copy_1::main (); operations_data_1::main (); operations_find_1::main (); operations_find_2::main (); operations_find_3::main (); operations_find_4::main (); operations_rfind_1::main (); operations_rfind_2::main (); operations_rfind_3::main (); operations_substr_1::main (); operators_2::main (); constexpr gdb::string_view sv_empty; SELF_CHECK (sv_empty.empty ()); std::string std_string = "fika"; gdb::string_view sv1 (std_string); SELF_CHECK (sv1 == "fika"); constexpr const char *fika = "fika"; gdb::string_view sv2 (fika); SELF_CHECK (sv2 == "fika"); constexpr gdb::string_view sv3 (fika, 3); SELF_CHECK (sv3 == "fik"); constexpr gdb::string_view sv4 (sv3); SELF_CHECK (sv4 == "fik"); constexpr gdb::string_view::iterator it_begin = sv4.begin (); static_assert (*it_begin == 'f', ""); constexpr gdb::string_view::iterator it_end = sv4.end (); static_assert (*it_end == 'a', ""); const gdb::string_view::reverse_iterator it_rbegin = sv4.rbegin (); SELF_CHECK (*it_rbegin == 'k'); const gdb::string_view::reverse_iterator it_rend = sv4.rend (); SELF_CHECK (*(it_rend - 1) == 'f'); constexpr gdb::string_view::size_type size = sv4.size (); static_assert (size == 3, ""); constexpr gdb::string_view::size_type length = sv4.length (); static_assert (length == 3, ""); constexpr gdb::string_view::size_type max_size = sv4.max_size (); static_assert (max_size > 0, ""); constexpr bool empty = sv4.empty (); static_assert (!empty, ""); constexpr char c1 = sv4[1]; static_assert (c1 == 'i', ""); constexpr char c2 = sv4.at (2); static_assert (c2 == 'k', ""); constexpr char front = sv4.front (); static_assert (front == 'f', ""); constexpr char back = sv4.back (); static_assert (back == 'k', ""); constexpr const char *data = sv4.data (); static_assert (data == fika, ""); }
static void test_move_constructor () { gdb_environ env; env.set ("A", "1"); SELF_CHECK (strcmp (env.get ("A"), "1") == 0); SELF_CHECK (set_contains (env.user_set_env (), std::string ("A=1"))); gdb_environ env2 = std::move (env); SELF_CHECK (env.envp ()[0] == NULL); SELF_CHECK (env.user_set_env ().size () == 0); SELF_CHECK (strcmp (env2.get ("A"), "1") == 0); SELF_CHECK (env2.envp ()[1] == NULL); SELF_CHECK (set_contains (env2.user_set_env (), std::string ("A=1"))); SELF_CHECK (env2.user_set_env ().size () == 1); env.set ("B", "2"); SELF_CHECK (strcmp (env.get ("B"), "2") == 0); SELF_CHECK (env.envp ()[1] == NULL); SELF_CHECK (set_contains (env.user_set_env (), std::string ("B=2"))); SELF_CHECK (env.user_set_env ().size () == 1); }