[Groonga-commit] ranguba/rroonga at 2903440 [master] Add Groonga::NotEqualOperator#exec

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Apr 13 16:49:11 JST 2015


Kouhei Sutou	2015-04-13 16:49:11 +0900 (Mon, 13 Apr 2015)

  New Revision: 29034404a8955bfff43d6488e898f71c239522e6
  https://github.com/ranguba/rroonga/commit/29034404a8955bfff43d6488e898f71c239522e6

  Message:
    Add Groonga::NotEqualOperator#exec

  Added files:
    ext/groonga/rb-grn-not-equal-operator.c
  Modified files:
    ext/groonga/rb-grn-operator.c
    ext/groonga/rb-grn.h
    test/test-operator.rb

  Added: ext/groonga/rb-grn-not-equal-operator.c (+85 -0) 100644
===================================================================
--- /dev/null
+++ ext/groonga/rb-grn-not-equal-operator.c    2015-04-13 16:49:11 +0900 (15d128d)
@@ -0,0 +1,85 @@
+/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+  Copyright (C) 2015  Kouhei Sutou <kou �� clear-code.com>
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License version 2.1 as published by the Free Software Foundation.
+
+  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
+*/
+
+#include "rb-grn.h"
+
+VALUE rb_cGrnNotEqualOperator;
+
+/*
+ * Executes a not-equal operation.
+ *
+ * @example Executes not-equal operations with the default context
+ *   Groonga::Operator::NOT_EQUAL.exec("hello", "Hello") # => true
+ *   Groonga::Operator::NOT_EQUAL.exec("hello", "hello") # => false
+ *
+ * @example Executes not-equal operations with the specified context
+ *   context = Groonga::Context.new
+ *   Groonga::Operator::NOT_EQUAL.exec("hello", "Hello",
+ *                                     :context => context) # => true
+ *   Groonga::Operator::NOT_EQUAL.exec("hello", "hello",
+ *                                     :context => context) # => false
+ *
+ * @overload exec(x, y, options={})
+ *   @param x [::Object] The left hand side value.
+ *   @param y [::Object] The right hand side value.
+ *   @param options [::Hash] The options.
+ *   @option options [Groonga::Context] (Groonga::Context.default)
+ *      The context to executes the operation.
+ *   @return [Boolean] `true` if `x` does not equal to `y`, `false`
+ *      otherwise.
+ */
+static VALUE
+rb_grn_not_equal_operator_exec (int argc, VALUE *argv, VALUE self)
+{
+    grn_bool equal;
+    VALUE rb_x;
+    VALUE rb_y;
+    VALUE rb_options;
+    VALUE rb_context;
+    grn_ctx *context;
+    grn_obj x;
+    grn_obj y;
+
+    rb_scan_args(argc, argv, "21", &rb_x, &rb_y, &rb_options);
+
+    rb_grn_scan_options(rb_options,
+                        "context", &rb_context,
+                        NULL);
+    context = rb_grn_context_ensure(&rb_context);
+
+    GRN_VOID_INIT(&x);
+    GRN_VOID_INIT(&y);
+    RVAL2GRNBULK(rb_x, context, &x);
+    RVAL2GRNBULK(rb_y, context, &y);
+    equal = grn_operator_exec_not_equal(context, &x, &y);
+    GRN_OBJ_FIN(context, &x);
+    GRN_OBJ_FIN(context, &y);
+
+    return CBOOL2RVAL(equal);
+}
+
+void
+rb_grn_init_not_equal_operator (VALUE mGrn)
+{
+    rb_cGrnNotEqualOperator = rb_define_class_under(mGrn,
+                                                    "NotEqualOperator",
+                                                    rb_cGrnOperator);
+
+    rb_define_method(rb_cGrnNotEqualOperator, "exec",
+                     rb_grn_not_equal_operator_exec, -1);
+}

  Modified: ext/groonga/rb-grn-operator.c (+2 -1)
===================================================================
--- ext/groonga/rb-grn-operator.c    2015-04-13 16:43:04 +0900 (226f2f7)
+++ ext/groonga/rb-grn-operator.c    2015-04-13 16:49:11 +0900 (feb202f)
@@ -302,6 +302,7 @@ rb_grn_init_operator (VALUE mGrn)
     rb_define_alias(rb_cGrnOperator, "to_int", "to_i");
 
     rb_grn_init_equal_operator(mGrn);
+    rb_grn_init_not_equal_operator(mGrn);
 
     rb_define_const(rb_cGrnOperator, "PUSH",
                     rb_funcall(rb_cGrnOperator, rb_intern("new"), 2,
@@ -429,7 +430,7 @@ rb_grn_init_operator (VALUE mGrn)
                                rb_str_new_cstr("equal"),
                                UINT2NUM(GRN_OP_EQUAL)));
     rb_define_const(rb_cGrnOperator, "NOT_EQUAL",
-                    rb_funcall(rb_cGrnOperator, rb_intern("new"), 2,
+                    rb_funcall(rb_cGrnNotEqualOperator, rb_intern("new"), 2,
                                rb_str_new_cstr("not-equal"),
                                UINT2NUM(GRN_OP_NOT_EQUAL)));
     rb_define_const(rb_cGrnOperator, "LESS",

  Modified: ext/groonga/rb-grn.h (+2 -0)
===================================================================
--- ext/groonga/rb-grn.h    2015-04-13 16:43:04 +0900 (baaf9f2)
+++ ext/groonga/rb-grn.h    2015-04-13 16:49:11 +0900 (5eaa6cb)
@@ -287,6 +287,7 @@ RB_GRN_VAR VALUE rb_cGrnSnippet;
 RB_GRN_VAR VALUE rb_cGrnVariable;
 RB_GRN_VAR VALUE rb_cGrnOperator;
 RB_GRN_VAR VALUE rb_cGrnEqualOperator;
+RB_GRN_VAR VALUE rb_cGrnNotEqualOperator;
 RB_GRN_VAR VALUE rb_cGrnExpression;
 RB_GRN_VAR VALUE rb_cGrnRecordExpressionBuilder;
 RB_GRN_VAR VALUE rb_cGrnColumnExpressionBuilder;
@@ -327,6 +328,7 @@ void           rb_grn_init_record                   (VALUE mGrn);
 void           rb_grn_init_variable                 (VALUE mGrn);
 void           rb_grn_init_operator                 (VALUE mGrn);
 void           rb_grn_init_equal_operator           (VALUE mGrn);
+void           rb_grn_init_not_equal_operator       (VALUE mGrn);
 void           rb_grn_init_expression               (VALUE mGrn);
 void           rb_grn_init_expression_builder       (VALUE mGrn);
 void           rb_grn_init_logger                   (VALUE mGrn);

  Modified: test/test-operator.rb (+21 -0)
===================================================================
--- test/test-operator.rb    2015-04-13 16:43:04 +0900 (7d21135)
+++ test/test-operator.rb    2015-04-13 16:49:11 +0900 (2029201)
@@ -41,4 +41,25 @@ class OperatorTest < Test::Unit::TestCase
       end
     end
   end
+
+  sub_test_case "not-equal" do
+    sub_test_case "#exec" do
+      test "not equal" do
+        assert_true(Groonga::Operator::NOT_EQUAL.exec("hello",
+                                                      "Hello"))
+      end
+
+      test "equal" do
+        assert_false(Groonga::Operator::NOT_EQUAL.exec("hello",
+                                                       "hello"))
+      end
+
+      test ":context" do
+        context = Groonga::Context.new
+        assert_true(Groonga::Operator::NOT_EQUAL.exec("hello",
+                                                      "Hello",
+                                                      :context => context))
+      end
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index