Ejemplo n.º 1
0
int main (int argc, char *argv[]) 
{
  const char *program = raptor_basename(argv[0]);
  raptor_world *world;
  const char *uri_string;
  raptor_www *www;
  const char *user_agent = "raptor_www_test/0.1";
  raptor_uri *uri;
  void *string = NULL;
  size_t string_length = 0;
  
  if(argc > 1)
    uri_string = argv[1];
  else
    uri_string = "http://librdf.org/";

  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);

  uri = raptor_new_uri(world, (const unsigned char*)uri_string);
  if(!uri) {
    fprintf(stderr, "%s: Failed to create Raptor URI for %s\n",
            program, uri_string);
    exit(1);
  }
  
  www = raptor_new_www(world);

  raptor_www_set_content_type_handler(www, write_content_type, (void*)stderr);
  raptor_www_set_user_agent(www, user_agent);

  /* start retrieval (always a GET) */
  
  if(raptor_www_fetch_to_string(www, uri,
                                &string, &string_length, malloc)) {
    fprintf(stderr, "%s: WWW fetch failed\n", program);
  } else {
#if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1    
    fprintf(stderr, "%s: HTTP response status %d\n",
            program, www->status_code);
    
    fprintf(stderr, "%s: Returned %d bytes of content\n",
            program, (int)string_length);
#endif
  }
  if(string)
    free(string);
  
  raptor_free_www(www);

  raptor_free_uri(uri);

  raptor_free_world(world);
  
  return 0;
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
  char fmt[FMT_LEN_MAX + 1];
  char arg[ARG_LEN_MAX + 1];
  size_t x, y;
  int errors = 0;

  program = raptor_basename(argv[0]);

  for(x = 2; x < FMT_LEN_MAX; x++) {
    for(y = 0; y < ARG_LEN_MAX; y++) {
      size_t len_ref = x + y - 2;

      /* fmt = "xxxxxxxx%s"
       * (number of 'x' characters varies)
       */
      memset(fmt, 'x', x - 2);
      fmt[x - 2] = '%';
      fmt[x - 1] = 's';
      fmt[x] = '\0';

      /* arg = "yyyyyyyy"
       * (number of 'y' characters varies)
       */
      memset(arg, 'y', y);
      arg[y] = '\0';

      /* assert(strlen(fmt) == x); */
      /* assert(strlen(arg) == y); */

      /* len_ref = sprintf(buf_ref, fmt, arg);
         assert((size_t)len_ref == x + y - 2); */

      IGNORE_FORMAT_NONLITERAL_START
      if(test_snprintf(len_ref, fmt, arg))
        errors++;
      IGNORE_FORMAT_NONLITERAL_END
    }
  }

  return errors;
}
Ejemplo n.º 3
0
int
main(int argc, char *argv[]) 
{
  raptor_world *world;
  const char *program = raptor_basename(argv[0]);
  const char *items[8] = { "ron", "amy", "jen", "bij", "jib", "daj", "jim", NULL };
  raptor_id_set *set;
  raptor_uri *base_uri;
  int i = 0;
  
  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);
    
  base_uri = raptor_new_uri(world, (const unsigned char*)"http://example.org/base#");

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Creating set\n", program);
#endif

  set = raptor_new_id_set(world);
  if(!set) {
    fprintf(stderr, "%s: Failed to create set\n", program);
    exit(1);
  }

  for(i = 0; items[i]; i++) {
    size_t len = strlen(items[i]);
    int rc;

#if RAPTOR_DEBUG > 1
    fprintf(stderr, "%s: Adding set item '%s'\n", program, items[i]);
#endif
  
    rc = raptor_id_set_add(set, base_uri, (const unsigned char*)items[i], len);
if(rc) {
      fprintf(stderr, "%s: Adding set item %d '%s' failed, returning error %d\n",
              program, i, items[i], rc);
      exit(1);
    }
  }

  for(i = 0; items[i]; i++) {
    size_t len = strlen(items[i]);
    int rc;

#if RAPTOR_DEBUG > 1
    fprintf(stderr, "%s: Adding duplicate set item '%s'\n", program, items[i]);
#endif

    rc = raptor_id_set_add(set, base_uri, (const unsigned char*)items[i], len);
    if(rc <= 0) {
      fprintf(stderr, "%s: Adding duplicate set item %d '%s' succeeded, should have failed, returning error %d\n",
              program, i, items[i], rc);
      exit(1);
    }
  }

#if RAPTOR_DEBUG > 1
  raptor_id_set_stats_print(set, stderr);
#endif

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Freeing set\n", program);
#endif
  raptor_free_id_set(set);

  raptor_free_uri(base_uri);
  
  raptor_free_world(world);
  
  /* keep gcc -Wall happy */
  return(0);
}
Ejemplo n.º 4
0
int
main(int argc, char *argv[]) 
{
  raptor_world *world;
  FILE *handle = NULL;
  int failures = 0;
  
  program = raptor_basename(argv[0]);

  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);
  
  /* Write tests */
  failures+= test_write_to_filename(world, (const char*)OUT_FILENAME,
                         TEST_STRING, TEST_STRING_LEN, (int)OUT_BYTES_COUNT);
  handle = fopen((const char*)OUT_FILENAME, "wb");
  if(!handle) {
    fprintf(stderr, "%s: Failed to create write file handle to file %s\n",
            program, OUT_FILENAME);
    failures++;
  } else {
    failures+= test_write_to_file_handle(world,
                                         handle, TEST_STRING, TEST_STRING_LEN,
                                         (int)OUT_BYTES_COUNT);
    fclose(handle);
    remove(OUT_FILENAME);
  }
  
  failures+= test_write_to_string(world,
                                  TEST_STRING,
                                  TEST_STRING_LEN, (int)OUT_BYTES_COUNT);
  failures+= test_write_to_sink(world,
                                TEST_STRING,
                                TEST_STRING_LEN, (int)OUT_BYTES_COUNT);

  remove(OUT_FILENAME);


  /* Read tests */
  handle = fopen((const char*)IN_FILENAME, "wb");
  if(!handle) {
    fprintf(stderr, "%s: Failed to create write handle to file %s\n",
            program, IN_FILENAME);
    failures++;
  } else {
    fwrite(TEST_STRING, 1, TEST_STRING_LEN, handle);
    fclose(handle);

    failures+= test_read_from_filename(world,
                                       (const char*)IN_FILENAME,
                                       TEST_STRING, TEST_STRING_LEN,
                                       TEST_STRING_LEN, 0);
    handle = fopen((const char*)IN_FILENAME, "rb");
    if(!handle) {
      fprintf(stderr, "%s: Failed to create read file handle to file %s\n",
              program, IN_FILENAME);
      failures++;
    } else {
      failures+= test_read_from_file_handle(world,
                                            handle,
                                            TEST_STRING, TEST_STRING_LEN,
                                            TEST_STRING_LEN, 0);
      fclose(handle); handle = NULL;
    }
  }
  
  failures+= test_read_from_string(world,
                                   TEST_STRING, TEST_STRING_LEN,
                                   TEST_STRING_LEN);
  failures+= test_read_from_sink(world, TEST_STRING_LEN, 0);

  remove(IN_FILENAME);
  
  raptor_free_world(world);
  
  return failures;
}
Ejemplo n.º 5
0
int
main(int argc, char *argv[]) 
{
  raptor_world *world;
  const char *program = raptor_basename(argv[0]);
  raptor_iostream *iostr;
  raptor_namespace_stack *nstack;
  raptor_namespace* foo_ns;
  raptor_xml_writer* xml_writer;
  raptor_uri* base_uri;
  raptor_qname* el_name;
  raptor_xml_element *element;
  unsigned long offset;
  raptor_qname **attrs;
  raptor_uri* base_uri_copy = NULL;

  /* for raptor_new_iostream_to_string */
  void *string = NULL;
  size_t string_len = 0;

  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);
  
  iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL);
  if(!iostr) {
    fprintf(stderr, "%s: Failed to create iostream to string\n", program);
    exit(1);
  }

  nstack = raptor_new_namespaces(world, 1);

  xml_writer = raptor_new_xml_writer(world, nstack, iostr);
  if(!xml_writer) {
    fprintf(stderr, "%s: Failed to create xml_writer to iostream\n", program);
    exit(1);
  }

  base_uri = raptor_new_uri(world, base_uri_string);

  foo_ns = raptor_new_namespace(nstack,
                              (const unsigned char*)"foo",
                              (const unsigned char*)"http://example.org/foo-ns#",
                              0);


  el_name = raptor_new_qname_from_namespace_local_name(world,
                                                       foo_ns,
                                                       (const unsigned char*)"bar", 
                                                       NULL);
  base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL;
  element = raptor_new_xml_element(el_name,
                                  NULL, /* language */
                                  base_uri_copy);

  raptor_xml_writer_start_element(xml_writer, element);
  raptor_xml_writer_cdata_counted(xml_writer, (const unsigned char*)"hello\n", 6);
  raptor_xml_writer_comment_counted(xml_writer, (const unsigned char*)"comment", 7);
  raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n");
  raptor_xml_writer_end_element(xml_writer, element);

  raptor_free_xml_element(element);

  raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n");

  el_name = raptor_new_qname(nstack, 
                             (const unsigned char*)"blah", 
                             NULL /* no attribute value - element */);
  base_uri_copy = base_uri ? raptor_uri_copy(base_uri) : NULL;
  element = raptor_new_xml_element(el_name,
                                   NULL, /* language */
                                   base_uri_copy);

  attrs = (raptor_qname **)RAPTOR_CALLOC(qnamearray, 1, sizeof(raptor_qname*));
  attrs[0] = raptor_new_qname(nstack, 
                              (const unsigned char*)"a",
                              (const unsigned char*)"b" /* attribute value */);
  raptor_xml_element_set_attributes(element, attrs, 1);

  raptor_xml_writer_empty_element(xml_writer, element);

  raptor_xml_writer_cdata(xml_writer, (const unsigned char*)"\n");

  raptor_free_xml_writer(xml_writer);

  raptor_free_xml_element(element);

  raptor_free_namespace(foo_ns);

  raptor_free_namespaces(nstack);

  raptor_free_uri(base_uri);

  
  offset = raptor_iostream_tell(iostr);

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Freeing iostream\n", program);
#endif
  raptor_free_iostream(iostr);

  if(offset != OUT_BYTES_COUNT) {
    fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program,
            (int)offset, (int)OUT_BYTES_COUNT);
    fputs("[[", stderr);
    (void)fwrite(string, 1, string_len, stderr);
    fputs("]]\n", stderr);
    return 1;
  }
  
  if(!string) {
    fprintf(stderr, "%s: I/O stream failed to create a string\n", program);
    return 1;
  }
  string_len = strlen((const char*)string);
  if(string_len != offset) {
    fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)offset);
    return 1;
  }

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Made XML string of %d bytes\n", program, (int)string_len);
  fputs("[[", stderr);
  (void)fwrite(string, 1, string_len, stderr);
  fputs("]]\n", stderr);
#endif

  raptor_free_memory(string);
  
  raptor_free_world(world);
  
  /* keep gcc -Wall happy */
  return(0);
}
Ejemplo n.º 6
0
int
main (int argc, char *argv[]) 
{
  const char *program = raptor_basename(argv[0]);
  const char *filename;
  FILE *fh;
  int rc = 0;
  unsigned int line = 1;
  size_t max_c2_len = 0;
  size_t max_c4_len = 0;
  int passes = 0;
  int fails = 0;

  if(argc != 2) {
    fprintf(stderr,
            "USAGE %s [path to NormalizationTest.txt]\n"
            "Get it at http://unicode.org/Public/UNIDATA/NormalizationTest.txt\n",
            program);
    return 1;
  }
  
  filename = argv[1];
  fh = fopen(filename, "r");
  if(!fh) {
    fprintf(stderr, "%s: file '%s' open failed - %s\n",
            program, filename, strerror(errno));
    return 1;
  }

#define LINE_BUFFER_SIZE 1024

/* FIXME big enough for Unicode 4 (c2 max 16; c4 max 33) */
#define UNISTR_SIZE 40

  for(;!feof(fh); line++) {
    char buffer[LINE_BUFFER_SIZE];
    char *p, *start;
    unsigned char column2[UNISTR_SIZE];
    unsigned char column4[UNISTR_SIZE];
    size_t column2_len, column4_len;
    int nfc_rc;
    int error;
    
    p = fgets(buffer, LINE_BUFFER_SIZE, fh);
    if(!p) {
      if(ferror(fh)) {
        fprintf(stderr, "%s: file '%s' read failed - %s\n",
                program, filename, strerror(errno));
        rc = 1;
        break;
      }
      /* assume feof */
      break;
    };

#if 0
    fprintf(stderr, "%s:%d: line '%s'\n", program, line, buffer);
#endif

    /* skip lines */
    if(*p == '@' || *p == '#')
      continue;

    if(line != 56)
      continue;
    

    /* skip column 1 */
    while(*p++ != ';')
      ;

    /* read column 2 into column2, column2_len */
    start = p;
    /* find end column 2 */
    while(*p++ != ';')
      ;

    column2_len = decode_to_utf8(column2, UNISTR_SIZE, start, p-1);
    if(column2_len > max_c2_len)
      max_c2_len = column2_len;
    
    /* skip column 3 */
    while(*p++ != ';')
      ;

    /* read column 4 into column4, column4_len */
    start = p;
    /* find end column 4 */
    while(*p++ != ';')
      ;

    column4_len = decode_to_utf8(column4, UNISTR_SIZE, start, p-1);
    if(column4_len > max_c4_len)
      max_c4_len = column4_len;

    if(!raptor_unicode_check_utf8_string(column2, column2_len)) {
      fprintf(stderr, "%s:%d: UTF8 column 2 failed on: '", filename, line);
      utf8_print(column2, column2_len, stderr);
      fputs("'\n", stderr);
      fails++;
    } else
      passes++;

    /* Column 2 must be NFC */
    nfc_rc = raptor_nfc_check(column2, column2_len, &error);
    if(!nfc_rc) {
      fprintf(stderr, "%s:%d: NFC column 2 failed on: '", filename, line);
      utf8_print(column2, column2_len, stderr);
      fprintf(stderr, "' at byte %d of %d\n", error, (int)column2_len);
      fails++;
    } else
      passes++;

    if(column2_len == column4_len && !memcmp(column2, column4, column2_len))
      continue;

    if(!raptor_unicode_check_utf8_string(column4, column4_len)) {
      fprintf(stderr, "%s:%d: UTF8 column 4 failed on: '", filename, line);
      utf8_print(column4, column4_len, stderr);
      fputs("'\n", stderr);
      fails++;
    } else
      passes++;

    /* Column 4 must be in NFC */
    nfc_rc = raptor_nfc_check(column4, column4_len, &error);
    if(!nfc_rc) {
      fprintf(stderr, "%s:%d: NFC column 4 failed on: '", filename, line);
      utf8_print(column4, column4_len, stderr);
      fprintf(stderr, "' at byte %d of %d\n", error, (int)column4_len);
      fails++;
    } else
      passes++;
  }

  fclose(fh);

  fprintf(stderr, "%s: max column 2 len: %d,  max column 4 len: %d\n", program,
          (int)max_c2_len, (int)max_c4_len);
  fprintf(stderr, "%s: passes: %d fails: %d\n", program,
          passes, fails);

  return rc;
}
Ejemplo n.º 7
0
int
main(int argc, char *argv[])
{
    raptor_world *world;
    const char *program = raptor_basename(argv[0]);
#define ITEM_COUNT 8
    const char *items[ITEM_COUNT+1] = { "ron", "amy", "jen", "bij", "jib", "daj", "jim", "def", NULL };
#define DELETE_COUNT 2
    const char *delete_items[DELETE_COUNT+1] = { "jen", "jim", NULL };
#define RESULT_COUNT (ITEM_COUNT-DELETE_COUNT)
    const char *results[RESULT_COUNT+1] = { "amy", "bij", "daj", "def", "jib", "ron", NULL};

    raptor_avltree* tree;
    raptor_avltree_iterator* iter;
    visit_state vs;
    int i;

    world = raptor_new_world();
    if(!world || raptor_world_open(world))
        exit(1);

    tree = raptor_new_avltree(compare_strings,
                              NULL, /* no free as they are static pointers above */
                              0);
    if(!tree) {
        fprintf(stderr, "%s: Failed to create tree\n", program);
        exit(1);
    }
    for(i = 0; items[i]; i++) {
        int rc;
        void* node;

#if RAPTOR_DEBUG > 1
        fprintf(stderr, "%s: Adding tree item '%s'\n", program, items[i]);
#endif

        rc = raptor_avltree_add(tree, (void*)items[i]);
        if(rc) {
            fprintf(stderr,
                    "%s: Adding tree item %d '%s' failed, returning error %d\n",
                    program, i, items[i], rc);
            exit(1);
        }

#ifdef RAPTOR_DEBUG
        raptor_avltree_check(tree);
#endif

        node = raptor_avltree_search(tree, (void*)items[i]);
        if(!node) {
            fprintf(stderr,
                    "%s: Tree did NOT contain item %d '%s' as expected\n",
                    program, i, items[i]);
            exit(1);
        }
    }

#if RAPTOR_DEBUG > 1
    fprintf(stderr, "%s: Printing tree\n", program);
    vs.fh = stderr;
    vs.count = 0;
    raptor_avltree_visit(tree, print_string, &vs);

    fprintf(stderr, "%s: Dumping tree\n", program);
    raptor_avltree_dump(tree, stderr);
#endif



    for(i = 0; delete_items[i]; i++) {
        int rc;

#if RAPTOR_DEBUG > 1
        fprintf(stderr, "%s: Deleting tree item '%s'\n", program, delete_items[i]);
#endif

        rc = raptor_avltree_delete(tree, (void*)delete_items[i]);
        if(!rc) {
            fprintf(stderr,
                    "%s: Deleting tree item %d '%s' failed, returning error %d\n",
                    program, i, delete_items[i], rc);
            exit(1);
        }

#ifdef RAPTOR_DEBUG
        raptor_avltree_check(tree);
#endif
    }


#if RAPTOR_DEBUG > 1
    fprintf(stderr, "%s: Walking tree forwards via iterator\n", program);
#endif
    iter = raptor_new_avltree_iterator(tree, NULL, NULL, 1);
    for(i = 0; 1; i++) {
        const char* data = (const char*)raptor_avltree_iterator_get(iter);
        const char* result = results[i];
        if((!data && data != result) || (data && strcmp(data, result))) {
            fprintf(stderr, "%3d: Forwards iterator expected '%s' but found '%s'\n",
                    i, result, data);
            exit(1);
        }
#if RAPTOR_DEBUG > 1
        fprintf(stderr, "%3d: Got '%s'\n", i, data);
#endif
        if(raptor_avltree_iterator_next(iter))
            break;
        if(i > RESULT_COUNT) {
            fprintf(stderr, "Forward iterator did not end on result %i as expected\n", i);
            exit(1);
        }
    }
    raptor_free_avltree_iterator(iter);


#if RAPTOR_DEBUG > 1
    fprintf(stderr, "%s: Checking tree\n", program);
#endif
    vs.count = 0;
    vs.results = results;
    vs.failed = 0;
    raptor_avltree_visit(tree, check_string, &vs);
    if(vs.failed) {
        fprintf(stderr, "%s: Checking tree failed\n", program);
        exit(1);
    }


    for(i = 0; results[i]; i++) {
        const char* result = results[i];
        char* data = (char*)raptor_avltree_remove(tree, (void*)result);
        if(!data) {
            fprintf(stderr, "%s: remove %i failed at item '%s'\n", program, i,
                    result);
            exit(1);
        }
        if(strcmp(data, result)) {
            fprintf(stderr, "%s: remove %i returned %s not %s as expected\n", program,
                    i, data, result);
            exit(1);
        }
    }


#if RAPTOR_DEBUG > 1
    fprintf(stderr, "%s: Freeing tree\n", program);
#endif
    raptor_free_avltree(tree);

    raptor_free_world(world);

    /* keep gcc -Wall happy */
    return(0);
}
Ejemplo n.º 8
0
int
main(int argc, char *argv[])
{
  raptor_world *world;
  const char *program = raptor_basename(argv[0]);
  int rc = 0;
  raptor_term* term1 = NULL; /* URI string 1 */
  raptor_term* term2 = NULL; /* literal string1 */
  raptor_term* term3 = NULL; /* blank node 1 */
  raptor_term* term4 = NULL; /* URI string 2 */
  raptor_term* term5 = NULL; /* URI string 1 again */
  raptor_uri* uri1;
  unsigned char* uri_str;
  size_t uri_len;
  
  
  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);


  /* check a term for NULL URI fails */
  term1 = raptor_new_term_from_uri(world, NULL);
  if(term1) {
    fprintf(stderr, "%s: raptor_new_uri(NULL) returned object rather than failing\n", program);
    rc = 1;
    goto tidy;
  }

  /* check a term for non-NULL URI succeeds */
  uri1 = raptor_new_uri(world, uri_string1);
  if(!uri1) {
    fprintf(stderr, "%s: raptor_new_uri(%s) failed\n", program, uri_string1);
    rc = 1;
    goto tidy;
  }
  term1 = raptor_new_term_from_uri(world, uri1);
  if(!term1) {
    fprintf(stderr, "%s: raptor_new_term_from_uri_string(URI %s) failed\n",
            program, uri_string1);
    rc = 1;
    goto tidy;
  }
  raptor_free_uri(uri1); uri1 = NULL;
  if(term1->type != uri_string1_type) {
    fprintf(stderr, "%s: raptor term 1 is of type %d expected %d\n",
            program, term1->type, uri_string1_type);
    rc = 1;
    goto tidy;
  }


  /* returns a pointer to shared string */
  uri_str = raptor_uri_as_counted_string(term1->value.uri, &uri_len);
  if(!uri_str) {
    fprintf(stderr, "%s: raptor_uri_as_counted_string term 1 failed\n",
            program);
    rc = 1;
    goto tidy;
  }

  if(uri_len != uri_string1_len) {
    fprintf(stderr, "%s: raptor term 1 URI is of length %d expected %d\n",
            program, (int)uri_len, (int)uri_string1_len);
    rc = 1;
    goto tidy;
  }

  
  /* check an empty literal is created from a NULL literal pointer succeeds */
  term2 = raptor_new_term_from_counted_literal(world, NULL, 0, NULL, NULL, 0);
  if(!term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal() with all NULLs failed\n", program);
    rc = 1;
    goto tidy;
  }
  raptor_free_term(term2);


  /* check an empty literal from an empty language literal pointer succeeds */
  term2 = raptor_new_term_from_counted_literal(world, NULL, 0, NULL,
                                               (const unsigned char*)"", 0);
  if(!term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal() with empty language failed\n", program);
    rc = 1;
    goto tidy;
  }
  raptor_free_term(term2);

  /* check a literal with language and datatype fails */
  uri1 = raptor_new_uri(world, uri_string1);
  if(!uri1) {
    fprintf(stderr, "%s: raptor_new_uri(%s) failed\n", program, uri_string1);
    rc = 1;
    goto tidy;
  }
  term2 = raptor_new_term_from_counted_literal(world, literal_string1,
                                               literal_string1_len, 
                                               uri1, language1, 0);
  raptor_free_uri(uri1); uri1 = NULL;
  if(term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal() with language and datatype returned object rather than failing\n", program);
    rc = 1;
    goto tidy;
  }
  
  /* check a literal with no language and no datatype succeeds */
  term2 = raptor_new_term_from_counted_literal(world, literal_string1,
                                               literal_string1_len, NULL, NULL, 0);
  if(!term2) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_literal(%s) failed\n",
            program, literal_string1);
    rc = 1;
    goto tidy;
  }
  if(term2->type != literal_string1_type) {
    fprintf(stderr, "%s: raptor term 2 is of type %d expected %d\n",
            program, term2->type, literal_string1_type);
    rc = 1;
    goto tidy;
  }
  

  /* check a blank node term with NULL id generates a new identifier */
  term3 = raptor_new_term_from_counted_blank(world, NULL, 0);
  if(!term3) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_blank(NULL) failed\n",
            program);
    rc = 1;
    goto tidy;
  }
  if(term3->type != bnodeid1_type) {
    fprintf(stderr, "%s: raptor term 3 is of type %d expected %d\n",
            program, term3->type, bnodeid1_type);
    rc = 1;
    goto tidy;
  }
  raptor_free_term(term3);

  /* check a blank node term with an identifier succeeds */
  term3 = raptor_new_term_from_counted_blank(world, bnodeid1, bnodeid1_len);
  if(!term3) {
    fprintf(stderr, "%s: raptor_new_term_from_counted_blank(%s) failed\n",
            program, bnodeid1);
    rc = 1;
    goto tidy;
  }
  if(term3->type != bnodeid1_type) {
    fprintf(stderr, "%s: raptor term 3 is of type %d expected %d\n",
            program, term3->type, bnodeid1_type);
    rc = 1;
    goto tidy;
  }


  /* check a different URI term succeeds */
  term4 = raptor_new_term_from_counted_uri_string(world, uri_string2,
                                                  uri_string2_len);
  if(!term4) {
    fprintf(stderr,
            "%s: raptor_new_term_from_counted_uri_string(URI %s) failed\n",
            program, uri_string2);
    rc = 1;
    goto tidy;
  }
  if(term4->type != uri_string2_type) {
    fprintf(stderr, "%s: raptor term 4 is of type %d expected %d\n",
            program, term4->type, uri_string2_type);
    rc = 1;
    goto tidy;
  }
  /* returns a pointer to shared string */
  uri_str = raptor_uri_as_counted_string(term4->value.uri, &uri_len);
  if(!uri_str) {
    fprintf(stderr, "%s: raptor_uri_as_counted_string term 4 failed\n",
            program);
    rc = 1;
    goto tidy;
  }

  if(uri_len != uri_string2_len) {
    fprintf(stderr, "%s: raptor term 4 URI is of length %d expected %d\n",
            program, (int)uri_len, (int)uri_string2_len);
    rc = 1;
    goto tidy;
  }


  /* check the same URI term as term1 succeeds */
  term5 = raptor_new_term_from_uri_string(world, uri_string1);
  if(!term5) {
    fprintf(stderr, "%s: raptor_new_term_from_uri_string(URI %s) failed\n",
            program, uri_string1);
    rc = 1;
    goto tidy;
  }


  if(raptor_term_equals(term1, term2)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, literal %s) returned equal, expected not-equal\n",
            program, uri_string1, literal_string1);
    rc = 1;
    goto tidy;
  }

  if(raptor_term_equals(term1, term3)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, bnode %s) returned equal, expected not-equal\n",
            program, uri_string1, bnodeid1);
    rc = 1;
    goto tidy;
  }
  
  if(raptor_term_equals(term1, term4)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, URI %s) returned equal, expected not-equal\n",
            program, uri_string1, uri_string2);
    rc = 1;
    goto tidy;
  }
  
  if(!raptor_term_equals(term1, term5)) {
    fprintf(stderr, "%s: raptor_term_equals (URI %s, URI %s) returned not-equal, expected equal\n",
            program, uri_string1, uri_string1);
    rc = 1;
    goto tidy;
  }

  if(term1->value.uri != term5->value.uri) {
    fprintf(stderr, "%s: term1 and term5 URI objects returned not-equal pointers, expected equal\n",
            program);
    /* This is not necessarily a failure if the raptor_uri module has had
     * the URI interning disabled with
     *   raptor_world_set_flag(world, RAPTOR_WORLD_FLAG_URI_INTERNING, 0) 
     * however this test suite does not do that, so it is a failure here.
     */
    rc = 1;
    goto tidy;
  }
  

  tidy:
  if(term1)
    raptor_free_term(term1);
  if(term2)
    raptor_free_term(term2);
  if(term3)
    raptor_free_term(term3);
  if(term4)
    raptor_free_term(term4);
  if(term5)
    raptor_free_term(term5);
  
  raptor_free_world(world);

  return rc;
}
Ejemplo n.º 9
0
int
main(int argc, char *argv[]) 
{
  const char *program=raptor_basename(argv[0]);
  raptor_sequence* seq1=raptor_new_sequence(NULL, (raptor_sequence_print_handler*)raptor_sequence_print_string);
  raptor_sequence* seq2=raptor_new_sequence(NULL, (raptor_sequence_print_handler*)raptor_sequence_print_string);
  char *s;
  int i;

  if(raptor_sequence_pop(seq1) || raptor_sequence_unshift(seq1)) {
    fprintf(stderr, "%s: should not be able to pop/unshift from an empty sequence\n", program);
    exit(1);
  }

  raptor_sequence_set_at(seq1, 0, (void*)"first");

  raptor_sequence_push(seq1, (void*)"third");

  raptor_sequence_shift(seq1, (void*)"second");

  s=(char*)raptor_sequence_get_at(seq1, 0);
  assert_match_string(raptor_sequence_get_at, s, "second");

  s=(char*)raptor_sequence_get_at(seq1, 1);
  assert_match_string(raptor_sequence_get_at, s, "first");
  
  s=(char*)raptor_sequence_get_at(seq1, 2);
  assert_match_string(raptor_sequence_get_at, s, "third");
  
  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 3);

  fprintf(stderr, "%s: sequence after additions: ", program);
  raptor_sequence_print(seq1, stderr);
  fputc('\n', stderr);

  /* now made alphabetical i.e. first, second, third */
  raptor_sequence_sort(seq1, raptor_compare_strings);

  fprintf(stderr, "%s: sequence after sort: ", program);
  raptor_sequence_print(seq1, stderr);
  fputc('\n', stderr);

  s=(char*)raptor_sequence_pop(seq1);
  assert_match_string(raptor_sequence_get_at, s, "third");

  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 2);

  fprintf(stderr, "%s: sequence after pop: ", program);
  raptor_sequence_print(seq1, stderr);
  fputc('\n', stderr);

  s=(char*)raptor_sequence_unshift(seq1);
  assert_match_string(raptor_sequence_get_at, s, "first");

  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 1);

  fprintf(stderr, "%s: sequence after unshift: ", program);
  raptor_sequence_print(seq1, stderr);
  fputc('\n', stderr);

  s=(char*)raptor_sequence_get_at(seq1, 0);
  assert_match_string(raptor_sequence_get_at, s, "second");
  
  raptor_sequence_push(seq2, (void*)"first.2");
  if(raptor_sequence_join(seq2, seq1)) {
    fprintf(stderr, "%s: raptor_sequence_join failed\n", program);
    exit(1);
  }

  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 0);
  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq2), 2);

  raptor_free_sequence(seq1);
  raptor_free_sequence(seq2);

  /* test sequence growing */
  
  seq1=raptor_new_sequence(NULL, (raptor_sequence_print_handler*)raptor_sequence_print_string);
  for(i=0; i<100; i++)
    if(raptor_sequence_shift(seq1, (void*)"foo")) {
      fprintf(stderr, "%s: raptor_sequence_shift failed\n", program);
      exit(1);
    }
  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 100);
  for(i=0; i<100; i++)
    raptor_sequence_unshift(seq1);
  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 0);
  raptor_free_sequence(seq1);

  seq1=raptor_new_sequence(NULL, (raptor_sequence_print_handler*)raptor_sequence_print_string);
  for(i=0; i<100; i++)
    if(raptor_sequence_push(seq1, (void*)"foo")) {
      fprintf(stderr, "%s: raptor_sequence_push failed\n", program);
      exit(1);
    }
  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 100);
  for(i=0; i<100; i++)
    raptor_sequence_pop(seq1);
  assert_match_int(raptor_sequence_size, raptor_sequence_size(seq1), 0);
  raptor_free_sequence(seq1);

  return (0);
}
Ejemplo n.º 10
0
int
main(int argc, char *argv[]) 
{
  raptor_world *world;
  const char *program = raptor_basename(argv[0]);
  raptor_iostream *iostr;
  raptor_namespace_stack *nstack;
  raptor_namespace* ex_ns;
  raptor_turtle_writer* turtle_writer;
  raptor_uri* base_uri;
  raptor_qname* el_name;
  unsigned long count;
  raptor_uri* datatype;
  
  /* for raptor_new_iostream_to_string */
  void *string = NULL;
  size_t string_len = 0;

  world = raptor_new_world();
  if(!world || raptor_world_open(world))
    exit(1);
  
  iostr = raptor_new_iostream_to_string(world, &string, &string_len, NULL);
  if(!iostr) {
    fprintf(stderr, "%s: Failed to create iostream to string\n", program);
    exit(1);
  }

  nstack = raptor_new_namespaces(world, 1);

  base_uri = raptor_new_uri(world, base_uri_string);

  turtle_writer = raptor_new_turtle_writer(world, base_uri, 1, nstack, iostr);
  if(!turtle_writer) {
    fprintf(stderr, "%s: Failed to create turtle_writer to iostream\n", program);
    exit(1);
  }

  raptor_turtle_writer_set_option(turtle_writer, 
                                   RAPTOR_OPTION_WRITER_AUTO_INDENT, 1);

  ex_ns = raptor_new_namespace(nstack,
                              (const unsigned char*)"ex",
                              (const unsigned char*)"http://example.org/ns#",
                              0);


  raptor_turtle_writer_namespace_prefix(turtle_writer, ex_ns);

  raptor_turtle_writer_reference(turtle_writer, base_uri);
  
  raptor_turtle_writer_increase_indent(turtle_writer);
  raptor_turtle_writer_newline(turtle_writer);
  
  raptor_turtle_writer_raw(turtle_writer, (const unsigned char*)"ex:foo ");

  raptor_turtle_writer_quoted_counted_string(turtle_writer, longstr,
                                             strlen((const char*)longstr));
  raptor_turtle_writer_raw_counted(turtle_writer,
                                   (const unsigned char*)" ;", 2);
  raptor_turtle_writer_newline(turtle_writer);

  el_name = raptor_new_qname_from_namespace_local_name(world,
                                                       ex_ns,
                                                       (const unsigned char*)"bar", 
                                                       NULL);

  raptor_turtle_writer_qname(turtle_writer, el_name);
  raptor_free_qname(el_name);

  raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)" ", 1);

  datatype = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/2001/XMLSchema#decimal");
  raptor_turtle_writer_literal(turtle_writer, nstack,
                               (const unsigned char*)"10.0", NULL,
                               datatype);
  raptor_free_uri(datatype);

  raptor_turtle_writer_newline(turtle_writer);

  raptor_turtle_writer_decrease_indent(turtle_writer);

  raptor_turtle_writer_raw_counted(turtle_writer, (const unsigned char*)".", 1);
  raptor_turtle_writer_newline(turtle_writer);

  
  raptor_free_turtle_writer(turtle_writer);

  raptor_free_namespace(ex_ns);

  raptor_free_namespaces(nstack);

  raptor_free_uri(base_uri);

  
  count = raptor_iostream_tell(iostr);

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Freeing iostream\n", program);
#endif
  raptor_free_iostream(iostr);

  if(count != OUT_BYTES_COUNT) {
    fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program,
            (int)count, (int)OUT_BYTES_COUNT);
    fputs("[[", stderr);
    (void)fwrite(string, 1, string_len, stderr);
    fputs("]]\n", stderr);
    return 1;
  }
  
  if(!string) {
    fprintf(stderr, "%s: I/O stream failed to create a string\n", program);
    return 1;
  }
  string_len = strlen((const char*)string);
  if(string_len != count) {
    fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)count);
    return 1;
  }

#if RAPTOR_DEBUG > 1
  fprintf(stderr, "%s: Made Turtle string of %d bytes\n", program, (int)string_len);
  fputs("[[", stderr);
  (void)fwrite(string, 1, string_len, stderr);
  fputs("]]\n", stderr);
#endif

  raptor_free_memory(string);

  raptor_free_world(world);

  /* keep gcc -Wall happy */
  return(0);
}