mu/src/tests/test-mu-str.c

311 lines
6.5 KiB
C
Raw Normal View History

2010-01-28 21:21:57 +01:00
/*
2010-09-26 14:23:17 +02:00
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
2010-01-28 21:21:57 +01:00
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program 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. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#if HAVE_CONFIG_H
2010-01-28 21:21:57 +01:00
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include "test-mu-common.h"
2010-11-22 23:44:18 +01:00
#include "src/mu-str.h"
#include "src/mu-msg-prio.h"
2010-01-28 21:21:57 +01:00
static void
2010-11-22 23:44:18 +01:00
test_mu_str_date_01 (void)
2010-01-28 21:21:57 +01:00
{
struct tm *tmbuf;
char buf[64];
gchar *tmp;
time_t some_time;
some_time = 1234567890;
tmbuf = localtime (&some_time);
strftime (buf, 64, "%x", tmbuf);
2010-01-28 21:21:57 +01:00
/* $ date -ud@1234567890; Fri Feb 13 23:31:30 UTC 2009 */
2010-11-22 23:44:18 +01:00
g_assert_cmpstr (mu_str_date_s ("%x", some_time), ==, buf);
2010-01-28 21:21:57 +01:00
/* date -ud@987654321 Thu Apr 19 04:25:21 UTC 2001 */
some_time = 987654321;
tmbuf = localtime (&some_time);
strftime (buf, 64, "%c", tmbuf);
2010-11-22 23:44:18 +01:00
tmp = mu_str_date ("%c", some_time);
2010-01-28 21:21:57 +01:00
g_assert_cmpstr (tmp, ==, buf);
g_free (tmp);
}
static void
2010-11-22 23:44:18 +01:00
test_mu_str_size_01 (void)
2010-01-28 21:21:57 +01:00
{
struct lconv *lc;
char *tmp2;
lc = localeconv();
tmp2 = g_strdup_printf ("0%s0 kB", lc->decimal_point);
2010-11-22 23:44:18 +01:00
g_assert_cmpstr (mu_str_size_s (0), ==, tmp2);
2010-01-28 21:21:57 +01:00
g_free (tmp2);
tmp2 = g_strdup_printf ("100%s0 kB", lc->decimal_point);
2010-11-22 23:44:18 +01:00
g_assert_cmpstr (mu_str_size_s (100000), ==, tmp2);
2010-01-28 21:21:57 +01:00
g_free (tmp2);
tmp2 = g_strdup_printf ("1%s1 MB", lc->decimal_point);
2010-11-22 23:44:18 +01:00
g_assert_cmpstr (mu_str_size_s (1100*1000), ==, tmp2);
2010-01-28 21:21:57 +01:00
g_free (tmp2);
}
static void
2010-11-22 23:44:18 +01:00
test_mu_str_size_02 (void)
2010-01-28 21:21:57 +01:00
{
struct lconv *lc;
char *tmp1, *tmp2;
lc = localeconv();
tmp2 = g_strdup_printf ("1%s0 MB", lc->decimal_point);
2010-11-22 23:44:18 +01:00
tmp1 = mu_str_size (999999);
2010-01-28 21:21:57 +01:00
g_assert_cmpstr (tmp1, !=, tmp2);
g_free (tmp1);
g_free (tmp2);
}
static void
2010-11-22 23:44:18 +01:00
test_mu_str_prio_01 (void)
2010-01-28 21:21:57 +01:00
{
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_LOW), ==, "low");
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_NORMAL), ==, "normal");
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_HIGH), ==, "high");
2010-01-28 21:21:57 +01:00
}
static gboolean
ignore_error (const char* log_domain, GLogLevelFlags log_level,
const gchar* msg, gpointer user_data)
2010-01-28 21:21:57 +01:00
{
return FALSE; /* don't abort */
}
static void
2010-11-22 23:44:18 +01:00
test_mu_str_prio_02 (void)
2010-01-28 21:21:57 +01:00
{
/* this must fail */
g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL);
g_assert_cmpstr (mu_msg_prio_name(666), ==, NULL);
2010-01-28 21:21:57 +01:00
}
static void
2010-11-22 23:44:18 +01:00
test_mu_str_normalize_01 (void)
{
int i;
struct {
const char* word;
const char* norm;
} words [] = {
{ "dantès", "dantes"},
{ "foo", "foo" },
{ "Föö", "foo" },
{ "číslo", "cislo" },
{ "hÆvý mëÐal ümláõt", "haevy medal umlaot"}
};
for (i = 0; i != G_N_ELEMENTS(words); ++i) {
gchar *str;
2010-11-22 23:44:18 +01:00
str = mu_str_normalize (words[i].word, TRUE);
g_assert_cmpstr (str, ==, words[i].norm);
g_free (str);
}
}
static void
test_mu_str_normalize_02 (void)
{
int i;
struct {
const char* word;
const char* norm;
} words [] = {
{ "DantèS", "DanteS"},
{ "foo", "foo" },
{ "Föö", "Foo" },
{ "číslO", "cislO" },
{ "hÆvý mëÐal ümláõt", "hAevy meDal umlaot"}
};
for (i = 0; i != G_N_ELEMENTS(words); ++i) {
gchar *str;
str = mu_str_normalize (words[i].word, FALSE);
g_assert_cmpstr (str, ==, words[i].norm);
g_free (str);
}
}
static void
test_mu_str_ascii_xapian_escape (void)
{
int i;
struct {
const char* word;
const char* esc;
} words [] = {
{ "aap@noot.mies", "aap_noot_mies"},
{ "Foo..Bar", "foo..bar" },
{ "subject:test@foo", "subject:test_foo" },
{ "xxx:test@bar", "xxx_test_bar" },
};
for (i = 0; i != G_N_ELEMENTS(words); ++i) {
gchar *a = g_strdup (words[i].word);
mu_str_ascii_xapian_escape_in_place (a);
g_assert_cmpstr (a, ==, words[i].esc);
g_free (a);
}
}
2010-11-23 21:05:55 +01:00
#if 0
static void
test_mu_str_complete_iso_date_begin (void)
{
int i;
struct {
const char* date1;
size_t len;
const char* date2;
} dates [] = {
{ "2010", 14, "20100101000000"},
{ "2009", 12, "200901010000" },
{ "19721214", 14, "19721214000000" },
{ "197212", 8, "19721201" },
};
for (i = 0; i != G_N_ELEMENTS(dates); ++i) {
gchar *str;
str = mu_str_complete_iso_date (dates[i].date1,
dates[i].len, TRUE);
g_assert_cmpstr (str, ==, dates[i].date2);
g_free (str);
}
}
static void
test_mu_str_complete_iso_date_end (void)
{
int i;
struct {
const char* date1;
size_t len;
const char* date2;
} dates [] = {
{ "2010", 14, "20101231235959"},
{ "2009", 12, "200912312359" },
{ "19721214", 14, "19721214235959" },
{ "197212", 8, "19721231" },
};
for (i = 0; i != G_N_ELEMENTS(dates); ++i) {
gchar *str;
str = mu_str_complete_iso_date (dates[i].date1,
dates[i].len, FALSE);
g_assert_cmpstr (str, ==, dates[i].date2);
g_free (str);
}
}
#endif
2010-01-28 21:21:57 +01:00
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
2010-11-22 23:44:18 +01:00
/* mu_str_date */
2010-11-23 21:05:55 +01:00
g_test_add_func ("/mu-str/mu-str-date",
2010-11-22 23:44:18 +01:00
test_mu_str_date_01);
2010-01-28 21:21:57 +01:00
2010-11-22 23:44:18 +01:00
/* mu_str_size */
2010-11-23 21:05:55 +01:00
g_test_add_func ("/mu-str/mu-str-size-01",
2010-11-22 23:44:18 +01:00
test_mu_str_size_01);
2010-11-23 21:05:55 +01:00
g_test_add_func ("/mu-str/mu-str-size-02",
2010-11-22 23:44:18 +01:00
test_mu_str_size_02);
2010-01-28 21:21:57 +01:00
2010-11-22 23:44:18 +01:00
/* mu_str_prio */
2010-11-23 21:05:55 +01:00
g_test_add_func ("/mu-str/mu-str-prio-01",
2010-11-22 23:44:18 +01:00
test_mu_str_prio_01);
2010-11-23 21:05:55 +01:00
g_test_add_func ("/mu-str/mu-str-prio-02",
2010-11-22 23:44:18 +01:00
test_mu_str_prio_02);
2010-01-28 21:21:57 +01:00
2010-11-22 23:44:18 +01:00
/* mu_str_normalize */
2010-11-23 21:05:55 +01:00
g_test_add_func ("/mu-str/mu-str-normalize-01",
2010-11-22 23:44:18 +01:00
test_mu_str_normalize_01);
g_test_add_func ("/mu-str/mu-str-normalize-02",
test_mu_str_normalize_02);
g_test_add_func ("/mu-str/mu-str-ascii-xapian-escape",
test_mu_str_ascii_xapian_escape);
2010-11-23 21:05:55 +01:00
/* mu_str_complete_iso_date_(begin|end) */
/* g_test_add_func ("/mu-str/mu-str-complete-iso-date-begin", */
/* test_mu_str_complete_iso_date_begin); */
/* g_test_add_func ("/mu-str/mu-str-complete-iso-date-begin", */
/* test_mu_str_complete_iso_date_end); */
2010-11-22 23:44:18 +01:00
/* FIXME: add tests for mu_str_flags; but note the
2010-01-28 21:21:57 +01:00
* function simply calls mu_msg_field_str */
g_log_set_handler (NULL,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,
2010-09-26 14:23:17 +02:00
(GLogFunc)black_hole, NULL);
2010-01-28 21:21:57 +01:00
return g_test_run ();
}