Blame tests/brl_checks.c

Packit c06654
#include <stdio.h>
Packit c06654
#include <string.h>
Packit c06654
#include <stdlib.h>
Packit c06654
#include <assert.h>
Packit c06654
#include "liblouis.h"
Packit c06654
#include "louis.h"
Packit c06654
#include "brl_checks.h"
Packit c06654
Packit c06654
int check_with_mode(
Packit c06654
    const char *tableList, const char *str, const char *typeform,
Packit c06654
    const char *expected, int mode, int direction);
Packit c06654
Packit c06654
void
Packit c06654
print_int_array(const char *prefix, int *pos_list, int len)
Packit c06654
{
Packit c06654
  int i;
Packit c06654
  printf("%s ", prefix);
Packit c06654
  for (i = 0; i < len; i++)
Packit c06654
    printf("%d ", pos_list[i]);
Packit c06654
  printf("\n");
Packit c06654
}
Packit c06654
Packit c06654
void
Packit c06654
print_widechars(widechar * buf, int len)
Packit c06654
{
Packit c06654
  int i;
Packit c06654
  for (i = 0; i < len; i++)
Packit c06654
    printf("%c", buf[i]);
Packit c06654
}
Packit c06654
Packit c06654
/* Helper function to convert a typeform string of '0's, '1's, '2's etc.
Packit c06654
   to the required format, which is an array of 0s, 1s, 2s, etc.
Packit c06654
   For example, "0000011111000" is converted to {0,0,0,0,0,1,1,1,1,1,0,0,0}
Packit c06654
   The caller is responsible for freeing the returned array. */
Packit c06654
char *
Packit c06654
convert_typeform(const char* typeform_string)
Packit c06654
{
Packit c06654
  int len = strlen(typeform_string);
Packit c06654
  char *typeform = malloc(len * sizeof(char));
Packit c06654
  int i;
Packit c06654
  for (i=0; i
Packit c06654
    typeform[i] = typeform_string[i] - '0';
Packit c06654
  return typeform;
Packit c06654
}
Packit c06654
Packit c06654
/* Check if a string is translated as expected. Return 0 if the
Packit c06654
   translation is as expected and 1 otherwise. */
Packit c06654
int
Packit c06654
check_translation(const char *tableList, const char *str,
Packit c06654
		  const char *typeform, const char *expected)
Packit c06654
{
Packit c06654
  return check_translation_with_mode(tableList, str, typeform, expected, 0);
Packit c06654
}
Packit c06654
Packit c06654
/* Check if a string is translated as expected. Return 0 if the
Packit c06654
   translation is as expected and 1 otherwise. */
Packit c06654
int
Packit c06654
check_translation_with_mode(const char *tableList, const char *str,
Packit c06654
			    const char *typeform, const char *expected, 
Packit c06654
			    int mode)
Packit c06654
{
Packit c06654
    return check_with_mode(tableList, str, typeform, expected, mode, 0);
Packit c06654
}
Packit c06654
Packit c06654
/* Check if a string is backtranslated as expected. Return 0 if the
Packit c06654
   backtranslation is as expected and 1 otherwise. */
Packit c06654
int
Packit c06654
check_backtranslation(const char *tableList, const char *str,
Packit c06654
		  const char *typeform, const char *expected)
Packit c06654
{
Packit c06654
  return check_backtranslation_with_mode(tableList, str, typeform, expected, 0);
Packit c06654
}
Packit c06654
Packit c06654
/* Check if a string is backtranslated as expected. Return 0 if the
Packit c06654
   backtranslation is as expected and 1 otherwise. */
Packit c06654
int
Packit c06654
check_backtranslation_with_mode(const char *tableList, const char *str,
Packit c06654
			    const char *typeform, const char *expected, 
Packit c06654
			    int mode)
Packit c06654
{
Packit c06654
    return check_with_mode(tableList, str, typeform, expected, mode, 1);
Packit c06654
}
Packit c06654
Packit c06654
/* direction, 0=forward, otherwise backwards */
Packit c06654
int check_with_mode(
Packit c06654
    const char *tableList, const char *str, const char *typeform,
Packit c06654
    const char *expected, int mode, int direction)
Packit c06654
{
Packit c06654
  widechar *inbuf;
Packit c06654
  widechar *outbuf;
Packit c06654
  int inlen;
Packit c06654
  int outlen;
Packit c06654
  int i, rv = 0;
Packit c06654
  int funcStatus = 0;
Packit c06654
Packit c06654
  char *typeformbuf = NULL;
Packit c06654
Packit c06654
  int expectedlen = strlen(expected);
Packit c06654
Packit c06654
  inlen = strlen(str);
Packit c06654
  outlen = inlen * 10;
Packit c06654
  inbuf = malloc(sizeof(widechar) * inlen);
Packit c06654
  outbuf = malloc(sizeof(widechar) * outlen);
Packit c06654
  if (typeform != NULL)
Packit c06654
    {
Packit c06654
      typeformbuf = malloc(outlen);
Packit c06654
      memcpy(typeformbuf, typeform, outlen);
Packit c06654
    }
Packit c06654
  inlen = extParseChars(str, inbuf);
Packit c06654
  if (!inlen)
Packit c06654
    {
Packit c06654
      printf("Cannot parse input string.\n");
Packit c06654
      return 1;
Packit c06654
    }
Packit c06654
  if (direction == 0)
Packit c06654
    {
Packit c06654
      funcStatus = lou_translate(tableList, inbuf, &inlen, outbuf, &outlen,
Packit c06654
		     typeformbuf, NULL, NULL, NULL, NULL, mode);
Packit c06654
    } else {
Packit c06654
      funcStatus = lou_backTranslate(tableList, inbuf, &inlen, outbuf, &outlen,
Packit c06654
		     typeformbuf, NULL, NULL, NULL, NULL, mode);
Packit c06654
    }
Packit c06654
  if (!funcStatus)
Packit c06654
    {
Packit c06654
      printf("Translation failed.\n");
Packit c06654
      return 1;
Packit c06654
    }
Packit c06654
Packit c06654
  for (i = 0; i < outlen && i < expectedlen && expected[i] == outbuf[i]; i++);
Packit c06654
  if (i < outlen || i < expectedlen)
Packit c06654
    {
Packit c06654
      rv = 1;
Packit c06654
      outbuf[outlen] = 0;
Packit c06654
      printf("Input: '%s'\n", str);
Packit c06654
      printf("Expected: '%s'\n", expected);
Packit c06654
      printf("Received: '");
Packit c06654
      print_widechars(outbuf, outlen);
Packit c06654
      printf("'\n");
Packit c06654
      if (i < outlen && i < expectedlen) 
Packit c06654
	{
Packit c06654
	  printf("Diff: Expected '%c' but recieved '%c' in index %d\n",
Packit c06654
		 expected[i], outbuf[i], i);
Packit c06654
	}
Packit c06654
      else if (i < expectedlen)
Packit c06654
	{
Packit c06654
	  printf("Diff: Expected '%c' but recieved nothing in index %d\n",
Packit c06654
		 expected[i], i);
Packit c06654
	}
Packit c06654
      else 
Packit c06654
	{
Packit c06654
	  printf("Diff: Expected nothing but recieved '%c' in index %d\n",
Packit c06654
		  outbuf[i], i);
Packit c06654
	}
Packit c06654
    }
Packit c06654
Packit c06654
  free(inbuf);
Packit c06654
  free(outbuf);
Packit c06654
  free(typeformbuf);
Packit c06654
  lou_free();
Packit c06654
  return rv;
Packit c06654
}
Packit c06654
Packit c06654
int
Packit c06654
check_inpos(const char *tableList, const char *str, const int *expected_poslist)
Packit c06654
{
Packit c06654
  widechar *inbuf;
Packit c06654
  widechar *outbuf;
Packit c06654
  int *inpos;
Packit c06654
  int inlen;
Packit c06654
  int outlen;
Packit c06654
  int i, rv = 0;
Packit c06654
Packit c06654
  inlen = strlen(str) * 2;
Packit c06654
  outlen = inlen;
Packit c06654
  inbuf = malloc(sizeof(widechar) * inlen);
Packit c06654
  outbuf = malloc(sizeof(widechar) * outlen);
Packit c06654
  inpos = malloc(sizeof(int) * inlen);
Packit c06654
  for (i = 0; i < inlen; i++)
Packit c06654
    {
Packit c06654
      inbuf[i] = str[i];
Packit c06654
    }
Packit c06654
  lou_translate(tableList, inbuf, &inlen, outbuf, &outlen,
Packit c06654
		NULL, NULL, NULL, inpos, NULL, 0);
Packit c06654
  for (i = 0; i < outlen; i++)
Packit c06654
    {
Packit c06654
      if (expected_poslist[i] != inpos[i])
Packit c06654
	{
Packit c06654
	  rv = 1;
Packit c06654
	  printf("Expected %d, recieved %d in index %d\n",
Packit c06654
		 expected_poslist[i], inpos[i], i);
Packit c06654
	}
Packit c06654
    }
Packit c06654
Packit c06654
  free(inbuf);
Packit c06654
  free(outbuf);
Packit c06654
  free(inpos);
Packit c06654
  lou_free();
Packit c06654
  return rv;
Packit c06654
Packit c06654
}
Packit c06654
Packit c06654
int
Packit c06654
check_outpos(const char *tableList, const char *str, const int *expected_poslist)
Packit c06654
{
Packit c06654
  widechar *inbuf;
Packit c06654
  widechar *outbuf;
Packit c06654
  int *inpos, *outpos;
Packit c06654
  int origInlen, inlen;
Packit c06654
  int outlen;
Packit c06654
  int i, rv = 0;
Packit c06654
Packit c06654
  origInlen = inlen = strlen(str);
Packit c06654
  outlen = inlen * 2;
Packit c06654
  inbuf = malloc(sizeof(widechar) * inlen);
Packit c06654
  outbuf = malloc(sizeof(widechar) * outlen);
Packit c06654
  /* outputPos can be affected by inputPos, so pass inputPos as well. */
Packit c06654
  inpos = malloc(sizeof(int) * outlen);
Packit c06654
  outpos = malloc(sizeof(int) * inlen);
Packit c06654
  for (i = 0; i < inlen; i++)
Packit c06654
    {
Packit c06654
      inbuf[i] = str[i];
Packit c06654
    }
Packit c06654
  lou_translate(tableList, inbuf, &inlen, outbuf, &outlen,
Packit c06654
		NULL, NULL, outpos, inpos, NULL, 0);
Packit c06654
  if (inlen != origInlen)
Packit c06654
    {
Packit c06654
      printf("original inlen %d and returned inlen %d differ\n",
Packit c06654
        origInlen, inlen);
Packit c06654
    }
Packit c06654
Packit c06654
  for (i = 0; i < inlen; i++)
Packit c06654
    {
Packit c06654
      if (expected_poslist[i] != outpos[i])
Packit c06654
	{
Packit c06654
	  rv = 1;
Packit c06654
	  printf("Expected %d, recieved %d in index %d\n",
Packit c06654
		 expected_poslist[i], outpos[i], i);
Packit c06654
	}
Packit c06654
    }
Packit c06654
Packit c06654
  free(inbuf);
Packit c06654
  free(outbuf);
Packit c06654
  free(inpos);
Packit c06654
  free(outpos);
Packit c06654
  lou_free();
Packit c06654
  return rv;
Packit c06654
Packit c06654
}
Packit c06654
Packit c06654
int
Packit c06654
check_cursor_pos(const char *str, const int *expected_pos)
Packit c06654
{
Packit c06654
  widechar *inbuf;
Packit c06654
  widechar *outbuf;
Packit c06654
  int *inpos, *outpos;
Packit c06654
  int orig_inlen;
Packit c06654
  int outlen;
Packit c06654
  int cursor_pos;
Packit c06654
  int i, rv = 0;
Packit c06654
Packit c06654
  orig_inlen = strlen(str);
Packit c06654
  outlen = orig_inlen;
Packit c06654
  inbuf = malloc(sizeof(widechar) * orig_inlen);
Packit c06654
  outbuf = malloc(sizeof(widechar) * orig_inlen);
Packit c06654
  inpos = malloc(sizeof(int) * orig_inlen);
Packit c06654
  outpos = malloc(sizeof(int) * orig_inlen);
Packit c06654
  for (i = 0; i < orig_inlen; i++)
Packit c06654
    {
Packit c06654
      inbuf[i] = str[i];
Packit c06654
    }
Packit c06654
Packit c06654
  for (i = 0; i < orig_inlen; i++)
Packit c06654
    {
Packit c06654
      int inlen = orig_inlen;
Packit c06654
      cursor_pos = i;
Packit c06654
      lou_translate(TRANSLATION_TABLE, inbuf, &inlen, outbuf, &outlen,
Packit c06654
		    NULL, NULL, NULL, NULL, &cursor_pos, compbrlAtCursor);
Packit c06654
      if (expected_pos[i] != cursor_pos)
Packit c06654
	{
Packit c06654
	  rv = 1;
Packit c06654
	  printf("string='%s' cursor=%d ('%c') expected=%d \
Packit c06654
recieved=%d ('%c')\n", str, i, str[i], expected_pos[i], cursor_pos, (char) outbuf[cursor_pos]);
Packit c06654
	}
Packit c06654
    }
Packit c06654
Packit c06654
  free(inbuf);
Packit c06654
  free(outbuf);
Packit c06654
  free(inpos);
Packit c06654
  free(outpos);
Packit c06654
  lou_free();
Packit c06654
Packit c06654
  return rv;
Packit c06654
}
Packit c06654
Packit c06654
/* Check if a string is hyphenated as expected. Return 0 if the
Packit c06654
   hyphenation is as expected and 1 otherwise. */
Packit c06654
int
Packit c06654
check_hyphenation(const char *tableList, const char *str, const char *expected)
Packit c06654
{
Packit c06654
  widechar *inbuf;
Packit c06654
  char *hyphens;
Packit c06654
  int inlen;
Packit c06654
  int rv = 0;
Packit c06654
Packit c06654
  inlen = strlen(str);
Packit c06654
  inbuf = malloc(sizeof(widechar) * inlen);
Packit c06654
  inlen = extParseChars(str, inbuf);
Packit c06654
  if (!inlen)
Packit c06654
    {
Packit c06654
      printf("Cannot parse input string.\n");
Packit c06654
      return 1;
Packit c06654
    }
Packit c06654
  hyphens = calloc(inlen+1, sizeof(char));
Packit c06654
Packit c06654
  if (!lou_hyphenate(tableList, inbuf, inlen, hyphens, 0))
Packit c06654
    {
Packit c06654
      printf("Hyphenation failed.\n");
Packit c06654
      return 1;
Packit c06654
    }
Packit c06654
Packit c06654
  if (strcmp(expected, hyphens))
Packit c06654
    {
Packit c06654
      printf("Input:    '%s'\n", str);
Packit c06654
      printf("Expected: '%s'\n", expected);
Packit c06654
      printf("Received: '%s'\n", hyphens);
Packit c06654
      rv = 1;
Packit c06654
    }
Packit c06654
Packit c06654
  free(inbuf);
Packit c06654
  free(hyphens);
Packit c06654
  lou_free();
Packit c06654
  return rv;
Packit c06654
Packit c06654
}