Fix a segfault
[glib.git] / gio / tests / g-file-info.c
blob80bdd6e463cadf4d5b758179fd32d2386ea118c5
1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #define TEST_NAME "Prilis zlutoucky kun"
29 #define TEST_DISPLAY_NAME "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88"
30 #define TEST_SIZE 0xFFFFFFF0
32 static void
33 test_assigned_values (GFileInfo *info)
35 const char *name, *display_name, *mistake;
36 guint64 size;
37 GFileType type;
39 /* Test for attributes presence */
40 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_NAME) == TRUE);
41 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME) == TRUE);
42 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE) == TRUE);
43 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME) == FALSE);
45 /* Retrieve data back and compare */
47 name = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME);
48 display_name = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME);
49 mistake = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
50 size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);
51 type = g_file_info_get_file_type (info);
53 g_assert_cmpstr (name, ==, TEST_NAME);
54 g_assert_cmpstr (display_name, ==, TEST_DISPLAY_NAME);
55 g_assert (mistake == NULL);
56 g_assert_cmpint (size, ==, TEST_SIZE);
57 g_assert_cmpstr (name, ==, g_file_info_get_name (info));
58 g_assert_cmpstr (display_name, ==, g_file_info_get_display_name (info));
59 g_assert_cmpint (size, ==, g_file_info_get_size (info) );
60 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
65 static void
66 test_g_file_info (void)
68 GFileInfo *info;
69 GFileInfo *info_dup;
70 GFileInfo *info_copy;
71 char **attr_list;
73 info = g_file_info_new ();
75 /* Test for empty instance */
76 attr_list = g_file_info_list_attributes (info, NULL);
77 g_assert (attr_list != NULL);
78 g_assert (*attr_list == NULL);
80 g_file_info_set_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME, TEST_NAME);
81 g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, TEST_DISPLAY_NAME);
82 g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE, TEST_SIZE);
83 g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
85 /* The attr list should not be empty now */
86 attr_list = g_file_info_list_attributes (info, NULL);
87 g_assert (attr_list != NULL);
88 g_assert (*attr_list != NULL);
90 test_assigned_values (info);
92 /* Test dups */
93 info_dup = g_file_info_dup (info);
94 g_assert (info_dup != NULL);
95 test_assigned_values (info_dup);
97 info_copy = g_file_info_new ();
98 g_file_info_copy_into (info_dup, info_copy);
99 g_assert (info_copy != NULL);
100 test_assigned_values (info_copy);
102 /* Test remove attribute */
103 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == FALSE);
104 g_file_info_set_attribute_int32 (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER, 10);
105 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == TRUE);
107 g_file_info_remove_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER);
108 g_assert (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SORT_ORDER) == FALSE);
110 g_object_unref (info);
111 g_object_unref (info_dup);
112 g_object_unref (info_copy);
116 main (int argc,
117 char *argv[])
119 g_type_init ();
120 g_test_init (&argc, &argv, NULL);
122 g_test_add_func ("/g-file-info/test_g_file_info", test_g_file_info);
124 return g_test_run();