📄 g-file.c
字号:
/* GLib testing framework examples and tests * Copyright (C) 2008 Red Hat, Inc. * Authors: Tomas Bzatek <tbzatek@redhat.com> * * This work is provided "as is"; redistribution and modification * in whole or in part, in any medium, physical or electronic is * permitted without restriction. * * This work is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * In no event shall the authors or contributors be liable for any * direct, indirect, incidental, special, exemplary, or consequential * damages (including, but not limited to, procurement of substitute * goods or services; loss of use, data, or profits; or business * interruption) however caused and on any theory of liability, whether * in contract, strict liability, or tort (including negligence or * otherwise) arising in any way out of the use of this software, even * if advised of the possibility of such damage. */#include <glib/glib.h>#include <gio/gio.h>#include <stdlib.h>#include <string.h>struct TestPathsWithOper { const char *path1; gboolean equal; gboolean use_uri; const char *path2; const char *path3;};/* TODO: * - test on Windows * **/static voidtest_g_file_new_null (void){ const char *paths[] = {"/", "/tmp///", "/non-existent-file", "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", NULL }; const char *uris[] = {"file:///", "file:///tmp///", "non-existent-uri:///some-dir/", "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", NULL }; GFile *file = NULL; int i = 0; while (paths[i]) { file = g_file_new_for_path (paths[i++]); g_assert (file != NULL); g_object_unref (file); } i = 0; while (uris[i]) { file = g_file_new_for_uri (uris[i++]); g_assert (file != NULL); g_object_unref(file); }}static gbooleancompare_two_files (const gboolean use_uri, const char *path1, const char *path2){ GFile *file1 = NULL; GFile *file2 = NULL; gboolean equal; if (use_uri) { file1 = g_file_new_for_uri (path1); file2 = g_file_new_for_uri (path2); } else { file1 = g_file_new_for_path (path1); file2 = g_file_new_for_path (path2); } g_assert (file1 != NULL); g_assert (file2 != NULL); equal = g_file_equal (file1, file2); g_object_unref (file1); g_object_unref (file2); return equal;}static voidtest_g_file_new_for_path (void){ const struct TestPathsWithOper cmp_paths[] = { {"/", TRUE, 0, "/./"}, {"//", TRUE, 0, "//"}, {"//", TRUE, 0, "//./"}, {"/", TRUE, 0, "/.//"}, {"/", TRUE, 0, "/././"}, {"/tmp", TRUE, 0, "/tmp/d/../"}, {"/", TRUE, 0, "/somedir/../"}, {"/", FALSE, 0, "/somedir/.../"}, {"//tmp/dir1", TRUE, 0, "//tmp/dir1"}, {"/tmp/dir1", TRUE, 0, "///tmp/dir1"}, {"/tmp/dir1", TRUE, 0, "////tmp/dir1"}, {"/tmp/dir1", TRUE, 0, "/tmp/./dir1"}, {"/tmp/dir1", TRUE, 0, "/tmp//dir1"}, {"/tmp/dir1", TRUE, 0, "/tmp///dir1///"}, {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, 0, "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/"} }; int i; for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++) { gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2); g_assert_cmpint (equal, ==, cmp_paths[i].equal); }}static voidtest_g_file_new_for_uri (void){ const struct TestPathsWithOper cmp_uris[] = { {"file:///", TRUE, 0, "file:///./"}, {"file:////", TRUE, 0, "file:////"}, {"file:////", TRUE, 0, "file:////./"}, {"file:///", TRUE, 0, "file:///.//"}, {"file:///", TRUE, 0, "file:///././"}, {"file:///tmp", TRUE, 0, "file:///tmp/d/../"}, {"file:///", TRUE, 0, "file:///somedir/../"}, {"file:///", FALSE, 0, "file:///somedir/.../"}, {"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1"}, {"file:///tmp/dir1", TRUE, 0, "file:///tmp/./dir1"}, {"file:///tmp/dir1", TRUE, 0, "file:///tmp//dir1"}, {"file:///tmp/dir1", TRUE, 0, "file:///tmp///dir1///"}, {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, 0, "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/"} }; int i; for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++) { gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2); g_assert_cmpint (equal, ==, cmp_uris[i].equal); }}static gbooleandup_equals (const gboolean use_uri, const char *path){ GFile *file1 = NULL; GFile *file2 = NULL; gboolean equal; if (use_uri) file1 = g_file_new_for_uri (path); else file1 = g_file_new_for_path (path); g_assert (file1 != NULL); file2 = g_file_dup (file1); g_assert (file2 != NULL); equal = g_file_equal (file1, file2); g_object_unref (file1); g_object_unref (file2); return equal;}static voidtest_g_file_dup (void){ const struct TestPathsWithOper dup_paths[] = { {"/", 0, FALSE, ""}, {"file:///", 0, TRUE, ""}, {"totalnonsense", 0, FALSE, ""}, {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, ""}, {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", 0, TRUE, ""}, }; int i; for (i = 0; i < G_N_ELEMENTS (dup_paths); i++) { gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1); g_assert (equal == TRUE); }}static gbooleanparse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name){ GFile *file1 = NULL; GFile *file2 = NULL; char *parsed_name; gboolean is_utf8_valid; gboolean equal; if (use_uri) file1 = g_file_new_for_uri (path); else file1 = g_file_new_for_path (path); g_assert (file1 != NULL); parsed_name = g_file_get_parse_name (file1); g_assert (parsed_name != NULL); /* UTF-8 validation */ is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL); g_assert (is_utf8_valid == TRUE); if (result_parse_name) g_assert_cmpstr (parsed_name, ==, result_parse_name); file2 = g_file_parse_name (parsed_name); g_assert (file2 != NULL); equal = g_file_equal (file1, file2); g_object_unref (file1); g_object_unref (file2); g_free (parsed_name); return equal;}static voidtest_g_file_get_parse_name_utf8 (void){ const struct TestPathsWithOper strings[] = { {"/", 0, FALSE, "/"}, {"file:///", 0, TRUE, "/"}, {"totalnonsense", 0, FALSE, NULL}, {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, NULL /* Depends on local file encoding */}, {"file:///invalid%08/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, TRUE, "file:///invalid%08/UTF-8%20p\xc5\x99\xc3\xadli\xc5\xa1%20\xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd%20k\xc5\xaf\xc5\x88"}, };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -