2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #error basename should not be defined at this point
27 static const char* gnu_basename(const char* in) {
33 #if !defined(basename)
34 #error basename should be defined at this point
37 static char* posix_basename(char* in) {
42 #include <gtest/gtest.h>
44 static void __TestGnuBasename(const char* in, const char* expected_out, int line) {
46 const char* out = gnu_basename(in);
47 ASSERT_STREQ(expected_out, out) << "(" << line << "): " << in << std::endl;
48 ASSERT_EQ(0, errno) << "(" << line << "): " << in << std::endl;
51 static void __TestPosixBasename(const char* in, const char* expected_out, int line) {
52 char* writable_in = (in != NULL) ? strdup(in) : NULL;
54 const char* out = posix_basename(&writable_in[0]);
55 ASSERT_STREQ(expected_out, out) << "(" << line << "): " << in << std::endl;
56 ASSERT_EQ(0, errno) << "(" << line << "): " << in << std::endl;
60 #define TestGnuBasename(in, expected) __TestGnuBasename(in, expected, __LINE__)
61 #define TestPosixBasename(in, expected) __TestPosixBasename(in, expected, __LINE__)
63 TEST(libgen_basename, gnu_basename) {
64 // GNU's basename doesn't accept NULL
65 // TestGnuBasename(NULL, ".");
66 TestGnuBasename("", "");
67 TestGnuBasename("/usr/lib", "lib");
68 TestGnuBasename("/system/bin/sh/", "");
69 TestGnuBasename("/usr/", "");
70 TestGnuBasename("usr", "usr");
71 TestGnuBasename("/", "");
72 TestGnuBasename(".", ".");
73 TestGnuBasename("..", "..");
74 TestGnuBasename("///", "");
75 TestGnuBasename("//usr//lib//", "");
78 TEST(libgen_basename, posix_basename) {
79 TestPosixBasename(NULL, ".");
80 TestPosixBasename("", ".");
81 TestPosixBasename("/usr/lib", "lib");
82 TestPosixBasename("/system/bin/sh/", "sh");
83 TestPosixBasename("/usr/", "usr");
84 TestPosixBasename("usr", "usr");
85 TestPosixBasename("/", "/");
86 TestPosixBasename(".", ".");
87 TestPosixBasename("..", "..");
88 TestPosixBasename("///", "/");
89 TestPosixBasename("//usr//lib//", "lib");