diff --git a/.gitignore b/.gitignore index 91fc0dc..7b9c5f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -SOURCES/libkkc-data-0.2.7.tar.xz +/tests/artifacts +/tests/libkkc-0.3.5.tar.gz.orig /libkkc-data-0.2.7.tar.xz diff --git a/gating.yaml b/gating.yaml new file mode 100644 index 0000000..fbd311f --- /dev/null +++ b/gating.yaml @@ -0,0 +1,7 @@ +--- !Policy +product_versions: + - rhel-8 +decision_context: osci_compose_gate +rules: + # this is the testcase identifier, which OSCI pipeline uses + - !PassingTestCaseRule {test_case_name: osci.brew-build.tier0.functional} diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..d3c7251 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,76 @@ +LIBKKC_VERSION = 0.3.5 +TOP_LIBKKC_BUILD_DIR = . +TOP_LIBKKC_SRC_DIR = . +#DEBUG_LIBS = -L$(TOP_LIBKKC_BUILD_DIR)/libkkc-$(LIBKKC_VERSION)/libkkc/.libs + +COMMON_CFLAGS = \ + -DHAVE_CONFIG_H -I. -pipe -Wall -Werror=format-security \ + -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong \ + -grecord-gcc-switches -m64 -mtune=generic -fasynchronous-unwind-tables \ + -fstack-clash-protection -fcf-protection -fPIC \ + -g -O2 \ + $(NULL) +PKG_CONFIG_CFLAGS = \ + $(shell pkg-config --cflags marisa gee-0.8 json-glib-1.0 glib-2.0) \ + -I$(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION) \ + $(NULL) +CFLAGS = $(COMMON_CFLAGS) $(PKG_CONFIG_CFLAGS) + +COMMON_LIBS = \ + -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 \ + -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong \ + -grecord-gcc-switches -m64 -mtune=generic -fasynchronous-unwind-tables \ + -fstack-clash-protection -fcf-protection -g -O0 \ + -Wl,-z -Wl,relro -Wl,-z -Wl,now \ + $(NULL) +PKG_CONFIG_LIBS = \ + $(shell pkg-config --libs marisa gee-0.8 json-glib-1.0 glib-2.0) \ + $(DEBUG_LIBS) -lkkc + $(NULL) +LDFLAGS = $(COMMON_LIBS) $(PKG_CONFIG_LIBS) + + +all: context + +context.c: context.vala + valac --disable-warnings --vapidir=. \ + --vapidir=$(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION)/libkkc \ + --pkg gobject-2.0 --pkg gio-2.0 --pkg gee-0.8 --pkg json-glib-1.0 \ + --pkg kkc-internals-1.0 -g --target-glib=2.24 \ + -C context.vala test-case.vala \ + $(NULL) + +context.o: context.c + gcc $(COMMON_CFLAGS) $(CFLAGS) -g -O0 -c -o context.o context.c + gcc $(COMMON_CFLAGS) $(CFLAGS) -g -O0 -c -o test-case.o test-case.c + +context: context.o + gcc $(LDFLAGS) -o context context.o test-case.o + +clean: + rm *.o *.c context + +run-debug: + env LD_LIBRARY_PATH=$(TOP_LIBKKC_BUILD_DIR)/libkkc-$(LIBKKC_VERSION)/libkkc/.libs gdb ./context + +run: + ./context + +data/models/sorted3/data.3gram.filter: + mkdir -p data/models/sorted3 + cp $(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION)/data/templates/libkkc-data/data/models/sorted3/metadata.json \ + data/models/sorted3/. + cp $(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION)/tests/data/models/text3/data.arpa \ + data/models/sorted3/. + python3 $(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION)/data/templates/libkkc-data/tools/sortlm.py \ + data/models/sorted3/data.arpa \ + data/models/sorted3/data + python3 $(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION)/data/templates/libkkc-data/tools/genfilter.py \ + data/models/sorted3/data.2gram \ + data/models/sorted3/data.2gram.filter \ + 12 + python3 $(TOP_LIBKKC_SRC_DIR)/libkkc-$(LIBKKC_VERSION)/data/templates/libkkc-data/tools/genfilter.py \ + data/models/sorted3/data.3gram \ + data/models/sorted3/data.3gram.filter \ + 10 + diff --git a/tests/context.vala b/tests/context.vala new file mode 100644 index 0000000..8e66d9b --- /dev/null +++ b/tests/context.vala @@ -0,0 +1,84 @@ +class ContextTests : Kkc.TestCase { + struct Conversion { + string sequence; + string result; + } + const Conversion eras[] = { + { "s h o u w a", "昭和" }, + { "h e i s e i", "平成" }, + { "r e i w a", "令和" }, + }; + Kkc.Context context; + + public ContextTests() { + base("Context"); + add_test("era candidate", this.test_eras_candidate); + add_test("era commit", this.test_eras_commit); + } + + void test_eras_candidate() { + foreach (var era in eras) + test_era_candidate(era); + } + + void test_eras_commit() { + foreach (var era in eras) + test_era_commit(era); + } + + private void test_era_candidate(Conversion era) { + context.process_key_events(era.sequence + " SPC SPC"); + assert(context.candidates.get(0).output == era.result); + context.process_key_events("SPC"); + assert (context.candidates.get(0).output == era.result); + context.reset (); + context.clear_output (); + } + + private void test_era_commit(Conversion era) { + context.process_key_events(era.sequence + " SPC RET"); + assert(context.has_output()); + assert(context.peek_output() == era.result); + assert(context.has_output()); + context.reset(); + context.clear_output(); + } + + public override void set_up() { + try { + // Loading /usr/lib64/libkkc/models/sorted3/metadata.json + var model = Kkc.LanguageModel.load("sorted3"); + context = new Kkc.Context(model); + } catch(Kkc.LanguageModelError e) { + stderr.printf("%s\n", e.message); + } + +#if 0 + try { + var srcdir = Environment.get_variable("srcdir"); + assert(srcdir != null); + var dictionary = new Kkc.SystemSegmentDictionary( + Path.build_filename(srcdir, "system-segment-dictionary")); + context.dictionaries.add(dictionary); + } catch(Error e) { + stderr.printf("%s\n", e.message); + } +#endif + + context.dictionaries.add(new Kkc.EmptySegmentDictionary()); + } + + public override void tear_down() { + context = null; + } +} + +int main(string[] args) { + Intl.setlocale(LocaleCategory.ALL, ""); + Test.init(ref args); + Kkc.init(); + TestSuite root = TestSuite.get_root(); + root.add_suite(new ContextTests().get_suite()); + Test.run(); + return 0; +} diff --git a/tests/prepare-source.sh b/tests/prepare-source.sh new file mode 100755 index 0000000..1262668 --- /dev/null +++ b/tests/prepare-source.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +LIBKKC_VERSION=0.3.5 + +if [ ! -d libkkc-$LIBKKC_VERSION ] ; then + wget https://github.com/ueno/libkkc/releases/download/v$LIBKKC_VERSION/libkkc-${LIBKKC_VERSION}.tar.gz + zcat libkkc-${LIBKKC_VERSION}.tar.gz | tar xfv - +fi + +if [ -f context.c ] ; then + rm *.c +fi + +if [ -f context.o ] ; then + rm *.o +fi + +if [ -f context ] ; then + rm context +fi + +pwd +ls +echo test +make diff --git a/tests/test-case.vala b/tests/test-case.vala new file mode 100644 index 0000000..da74f7e --- /dev/null +++ b/tests/test-case.vala @@ -0,0 +1,118 @@ +/* testcase.vala + * + * Copyright (C) 2009 Julien Peeters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library 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 + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Julien Peeters + * + * Copied from libgee/tests/testcase.vala. + */ + +public abstract class Kkc.TestCase : Object +{ + private GLib.TestSuite _suite; + private Adaptor[] _adaptors = new Adaptor[0]; + + public delegate void TestMethod (); + + public TestCase (string name) + { + this._suite = new GLib.TestSuite (name); + } + + public void add_test (string name, TestMethod test) + { + var adaptor = new Adaptor (name, test, this); + this._adaptors += adaptor; + + this._suite.add (new GLib.TestCase ( + adaptor.name, adaptor.set_up, adaptor.run, adaptor.tear_down)); + } + + public virtual void set_up () + { + } + + public virtual void tear_down () + { + } + + public GLib.TestSuite get_suite () + { + return this._suite; + } + + private class Adaptor + { + public string name { get; private set; } + private unowned TestMethod _test; + private TestCase _test_case; + + public Adaptor (string name, TestMethod test, TestCase test_case) + { + this._name = name; + this._test = test; + this._test_case = test_case; + } + + public void set_up (void* fixture) + { + GLib.set_printerr_handler (Adaptor._printerr_func_stack_trace); + Log.set_default_handler (this._log_func_stack_trace); + + this._test_case.set_up (); + } + + private static void _printerr_func_stack_trace (string? text) + { + if (text == null || str_equal (text, "")) + return; + + stderr.printf (text); + + /* Print a stack trace since we've hit some major issue */ + GLib.on_error_stack_trace ("libtool --mode=execute gdb"); + } + + private void _log_func_stack_trace (string? log_domain, + LogLevelFlags log_levels, + string message) + { + Log.default_handler (log_domain, log_levels, message); + + /* Print a stack trace for any message at the warning level or above + */ + if ((log_levels & + (LogLevelFlags.LEVEL_WARNING | LogLevelFlags.LEVEL_ERROR | + LogLevelFlags.LEVEL_CRITICAL)) + != 0) + { + GLib.on_error_stack_trace ("libtool --mode=execute gdb"); + } + } + + public void run (void* fixture) + { + this._test (); + } + + public void tear_down (void* fixture) + { + this._test_case.tear_down (); + } + } +} diff --git a/tests/tests.yml b/tests/tests.yml new file mode 100644 index 0000000..d4fa154 --- /dev/null +++ b/tests/tests.yml @@ -0,0 +1,31 @@ +- hosts: localhost + roles: + - role: standard-test-source + tags: + - always + - role: standard-test-basic + tags: + - classic + required_packages: + - bash + - gcc + - gzip + - libkkc + - libkkc-data + - make + - tar + - vala + - wget + - glib2-devel + - json-glib-devel + - libgee-devel + - libkkc-devel + - marisa-devel + tests: + - prepare-source: + dir: . + run: ./prepare-source.sh + - smoke1: + dir: . + run: ./context +