[Groonga-commit] groonga/groonga [master] truncate the last new line to compare correctly.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 1月 5日 (水) 16:08:04 JST


Kouhei Sutou	2011-01-05 07:08:04 +0000 (Wed, 05 Jan 2011)

  New Revision: bcd324b42fccc2d6a62720da1d898cdaab38a228

  Log:
    truncate the last new line to compare correctly.

  Modified files:
    src/grntest.c
    test/unit/grntest/test-out-http.rb

  Modified: src/grntest.c (+39 -46)
===================================================================
--- src/grntest.c    2011-01-05 03:43:11 +0000 (90cda59)
+++ src/grntest.c    2011-01-05 07:08:04 +0000 (ee05c7e)
@@ -452,59 +452,46 @@ error_command(grn_ctx *ctx, char *command, int task_id)
 }
 #endif
 
-static int
-diff_result(char *expect, int elen, char *result, int rlen)
+static void
+normalize_output(char *output, int length,
+                 char **normalized_output, int *normalized_length)
 {
   int i;
-  char *e = NULL, *r = NULL;
 
-  i = 0;
-  while(i < elen) {
-    if (!strncmp(&expect[i], "],", 2)) {
-      e = &expect[i] + 2;
+  *normalized_output = NULL;
+  *normalized_length = length;
+  for (i = 0; i < length; i++) {
+    if (!strncmp(output + i, "],", 2)) {
+      *normalized_output = output + i + 2;
+      *normalized_length -= i + 2;
       break;
     }
-    i++;
-  }
-  if (e == NULL) {
-/*
-    fputc('[', stderr);
-    fwrite(expect, 1, elen, stderr);
-    fputc(']', stderr);
-    fprintf(stderr, " is not groonga command output\n", );
-    return 1;
-*/
-    if (elen > 2 && strncmp(expect + elen - 2, "]]", 2)) {
-      e = expect + elen;
-    } else {
-      e = expect;
-    }
   }
 
-  i = 0;
-  while(i < rlen) {
-    if (!strncmp(&result[i], "],", 2)) {
-      r = &result[i] + 2;
-      break;
-    }
-    i++;
-  }
-  if (r == NULL) {
-/*
-    fputc('[', stderr);
-    fwrite(result, 1, rlen, stderr);
-    fputc(']', stderr);
-    fprintf(stderr, " is not groonga command output\n");
-    return 1;
-*/
-    if (rlen > 2 && strncmp(result + rlen - 2, "]]", 2)) {
-      r = result + rlen;
+  if (!*normalized_output) {
+    if (length > 2 && strncmp(output + length - 2, "]]", 2)) {
+      *normalized_output = output + length;
+      *normalized_length = 0;
     } else {
-      r = result;
+      *normalized_output = output;
     }
   }
+}
+
+static grn_bool
+same_result_p(char *expect, int expected_length, char *result, int result_length)
+{
+  char *normalized_expected, *normalized_result;
+  int normalized_expected_length, normalized_result_length;
 
-  return strncmp(e, r, strlen(e));
+  normalize_output(expect, expected_length,
+                   &normalized_expected, &normalized_expected_length);
+  normalize_output(result, result_length,
+                   &normalized_result, &normalized_result_length);
+
+  return((normalized_expected_length == normalized_result_length) &&
+         strncmp(normalized_expected, normalized_result,
+                 normalized_expected_length) == 0);
 }
 
 static socket_t
@@ -854,9 +841,12 @@ do_load_command(grn_ctx *ctx, char *command, int type, int task_id,
           GRN_LOG(ctx, GRN_ERROR, "Cannot get input-log");
           error_exit_in_thread(55);
         }
+        if (GRN_TEXT_VALUE(&log)[GRN_TEXT_LEN(&log) - 1] == '\n') {
+          grn_bulk_truncate(ctx, &log, GRN_TEXT_LEN(&log) - 1);
+        }
 
-        if (diff_result(GRN_TEXT_VALUE(&log), GRN_TEXT_LEN(&log),
-                        res, res_len)) {
+        if (!same_result_p(GRN_TEXT_VALUE(&log), GRN_TEXT_LEN(&log),
+                           res, res_len)) {
           fprintf(output, "DIFF:command:%s\n", command);
           fprintf(output, "DIFF:result:");
           fwrite(res, 1, res_len, output);
@@ -935,9 +925,12 @@ do_command(grn_ctx *ctx, char *command, int type, int task_id)
           GRN_LOG(ctx, GRN_ERROR, "Cannot get input-log");
           error_exit_in_thread(55);
         }
+        if (GRN_TEXT_VALUE(&log)[GRN_TEXT_LEN(&log) - 1] == '\n') {
+          grn_bulk_truncate(ctx, &log, GRN_TEXT_LEN(&log) - 1);
+        }
 
-        if (diff_result(GRN_TEXT_VALUE(&log), GRN_TEXT_LEN(&log),
-                        res, res_len)) {
+        if (!same_result_p(GRN_TEXT_VALUE(&log), GRN_TEXT_LEN(&log),
+                           res, res_len)) {
           fprintf(output, "DIFF:command:%s\n", command);
           fprintf(output, "DIFF:result:");
           fwrite(res, 1, res_len, output);

  Modified: test/unit/grntest/test-out-http.rb (+42 -5)
===================================================================
--- test/unit/grntest/test-out-http.rb    2011-01-05 03:43:11 +0000 (cc41df1)
+++ test/unit/grntest/test-out-http.rb    2011-01-05 07:08:04 +0000 (0916e27)
@@ -54,7 +54,7 @@ class GrnTestOutHTTPTest < Test::Unit::TestCase
                  [status[0], result])
   end
 
-  def test_test_http
+  def test_test_http_same
     command = 'select Shops --sortby _id --limit 5 --output_columns "name"'
     command_file = tempfile("command") do |file|
       file.puts(command)
@@ -78,12 +78,49 @@ class GrnTestOutHTTPTest < Test::Unit::TestCase
                                            "--groonga", groonga,
                                            "--protocol", "http",
                                            "--log-output-dir", @tmp_dir,
-                                           script_file.path, @database_path)
+                                           script_file.path,
+                                           @database_path)
+    assert_predicate(status, :success?, [output, error])
+    assert_equal("", File.read("#{expected_file.path}.diff"))
+  end
+
+  def test_test_http_diff
+    command = 'select Shops --sortby _id --limit 3 --output_columns "name"'
+    command_file = tempfile("command") do |file|
+      file.puts(command)
+    end
+    expected =
+      '[[0,1290511592.67556,0.00068249],' +
+      '[[[36],' +
+      '[["name","ShortText"]],' +
+      '["根津のたいやき"],' +
+      '["たい焼 カタオカ"],' +
+      '["そばたいやき空"],' +
+      '["車"],' +
+      '["広瀬屋"]]]]'
+    actual =
+      '[[0,1290511592.67556,0.00068249],' +
+      '[[[36],' +
+      '[["name","ShortText"]],' +
+      '["根津のたいやき"],' +
+      '["たい焼 カタオカ"],' +
+      '["そばたいやき空"]]]]'
+    expected_file = tempfile("expected") do |file|
+      file.puts(expected)
+    end
+    script_file = tempfile("script") do |file|
+      file.puts("test_http #{command_file.path} #{expected_file.path}")
+    end
+    output, error, status = invoke_grntest("--noftp",
+                                           "--groonga", groonga,
+                                           "--protocol", "http",
+                                           "--log-output-dir", @tmp_dir,
+                                           script_file.path,
+                                           @database_path)
     assert_predicate(status, :success?, [output, error])
     assert_equal("DIFF:command:#{command}\n" +
-                 "DIFF:result:#{normalize_result(expected)}\n" +
-                 "DIFF:expect:#{normalize_result(expected)}\n" +
-                 "\n",
+                 "DIFF:result:#{normalize_result(actual)}\n" +
+                 "DIFF:expect:#{normalize_result(expected)}\n",
                  normalize_result(File.read("#{expected_file.path}.diff")))
   end
 




Groonga-commit メーリングリストの案内
Back to archive index