示例#1
0
int
main (int   argc,
      char *argv[])
{
  guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
  guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
#ifdef G_HAVE_GINT64
  guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
	  gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
#endif

  /* type sizes */
  g_assert (sizeof (gint8) == 1);
  g_assert (sizeof (gint16) == 2);
  g_assert (sizeof (gint32) == 4);
#ifdef	G_HAVE_GINT64
  g_assert (sizeof (gint64) == 8);
#endif	/* G_HAVE_GINT64 */

  g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
  g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
#ifdef G_HAVE_GINT64
  g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
#endif

  return 0;
}
示例#2
0
文件: copy.c 项目: kjell/libvips
/* Swap 8- of bytes.
 */
static void
vips_copy_swap8( VipsPel *in, VipsPel *out, int width, VipsImage *im )
{
    guint64 *p = (guint64 *) in;
    guint64 *q = (guint64 *) out;
    int sz = (VIPS_IMAGE_SIZEOF_PEL( im ) * width) / 8;

    int x;

    for( x = 0; x < sz; x++ )
        q[x] = GUINT64_SWAP_LE_BE( p[x] );
}
示例#3
0
static inline gdouble
_gdouble_swap_le_be (gdouble * d)
{
  union
  {
    guint64 i;
    gdouble d;
  } u;

  u.d = *d;
  u.i = GUINT64_SWAP_LE_BE (u.i);
  return u.d;
}
示例#4
0
gboolean
qdisk_start(QDisk *self, const gchar *filename, GQueue *qout, GQueue *qbacklog, GQueue *qoverflow)
{
  gboolean new_file = FALSE;
  gpointer p = NULL;
  int openflags = 0;

  /*
   * If qdisk_start is called for already initialized qdisk file
   * it can cause message loosing.
   * We need this assert to detect programming error as soon as possible.
   */
  g_assert(!qdisk_initialized(self));

  if (self->options->disk_buf_size <= 0)
    return TRUE;

  if (self->options->read_only && !filename)
    return FALSE;

  if (!filename)
    {
      new_file = TRUE;
      /* NOTE: this'd be a security problem if we were not in our private directory. But we are. */
      filename = _next_filename(self);
    }
  else
    {
      struct stat st;
      if (stat(filename,&st) == -1)
        {
          new_file = TRUE;
        }
    }

  self->filename = g_strdup(filename);
  /* assumes self is zero initialized */
  openflags = self->options->read_only ? (O_RDONLY | O_LARGEFILE) : (O_RDWR | O_LARGEFILE | (new_file ? O_CREAT : 0));

  self->fd = open(filename, openflags, 0600);
  if (self->fd < 0)
    {
      msg_error("Error opening disk-queue file",
                evt_tag_str("filename", self->filename),
                evt_tag_errno("error", errno));
      return FALSE;
    }

  p = mmap(0, sizeof(QDiskFileHeader),  self->options->read_only ? (PROT_READ) : (PROT_READ | PROT_WRITE), MAP_SHARED, self->fd, 0);

  if (p == MAP_FAILED)
    {
      msg_error("Error returned by mmap",
                evt_tag_errno("errno", errno),
                evt_tag_str("filename", self->filename));
       return FALSE;
     }
   else
     {
       madvise(p, sizeof(QDiskFileHeader), MADV_RANDOM);
     }

  if (self->options->read_only)
    {
      self->hdr = g_malloc(sizeof(QDiskFileHeader));
      memcpy(self->hdr, p, sizeof(QDiskFileHeader));
      munmap(p, sizeof(QDiskFileHeader) );
      p = NULL;
    }
  else
    {
      self->hdr = p;
    }
  /* initialize new file */

  if (new_file)
    {
      QDiskFileHeader tmp;
      memset(&tmp, 0, sizeof(tmp));
      if (!pwrite_strict(self->fd, &tmp, sizeof(tmp), 0))
        {
          msg_error("Error occured while initalizing the new queue file",evt_tag_str("filename",self->filename),evt_tag_errno("error",errno));
          munmap((void *)self->hdr, sizeof(QDiskFileHeader));
          self->hdr = NULL;
          close(self->fd);
          self->fd = -1;
          return FALSE;
        }
      self->hdr->version = 1;
      self->hdr->big_endian = (G_BYTE_ORDER == G_BIG_ENDIAN);

      self->hdr->read_head = QDISK_RESERVED_SPACE;
      self->hdr->write_head = QDISK_RESERVED_SPACE;
      self->hdr->backlog_head = self->hdr->read_head;
      self->hdr->length = 0;

      if (!qdisk_save_state(self, qout, qbacklog, qoverflow))
        {
          munmap((void *)self->hdr, sizeof(QDiskFileHeader));
          self->hdr = NULL;
          close(self->fd);
          self->fd = -1;
          return FALSE;
        }
    }
  else
    {
      struct stat st;

      if (fstat(self->fd, &st) != 0 || st.st_size == 0)
        {
           msg_error("Error loading disk-queue file",
                     evt_tag_str("filename", self->filename),
                     evt_tag_errno("fstat error", errno),
                     evt_tag_int("size", st.st_size));
           munmap((void *)self->hdr, sizeof(QDiskFileHeader));
           self->hdr = NULL;
           close(self->fd);
           self->fd = -1;
           return FALSE;
        }
      if (self->hdr->version == 0)
        {
          self->hdr->big_endian = TRUE;
          self->hdr->version = 1;
          self->hdr->backlog_head = self->hdr->read_head;
          self->hdr->backlog_len = 0;
        }
      if ((self->hdr->big_endian && G_BYTE_ORDER == G_LITTLE_ENDIAN) ||
          (!self->hdr->big_endian && G_BYTE_ORDER == G_BIG_ENDIAN))
        {
          self->hdr->read_head = GUINT64_SWAP_LE_BE(self->hdr->read_head);
          self->hdr->write_head = GUINT64_SWAP_LE_BE(self->hdr->write_head);
          self->hdr->length = GUINT64_SWAP_LE_BE(self->hdr->length);
          self->hdr->qout_ofs = GUINT64_SWAP_LE_BE(self->hdr->qout_ofs);
          self->hdr->qout_len = GUINT32_SWAP_LE_BE(self->hdr->qout_len);
          self->hdr->qout_count = GUINT32_SWAP_LE_BE(self->hdr->qout_count);
          self->hdr->qbacklog_ofs = GUINT64_SWAP_LE_BE(self->hdr->qbacklog_ofs);
          self->hdr->qbacklog_len = GUINT32_SWAP_LE_BE(self->hdr->qbacklog_len);
          self->hdr->qbacklog_count = GUINT32_SWAP_LE_BE(self->hdr->qbacklog_count);
          self->hdr->qoverflow_ofs = GUINT64_SWAP_LE_BE(self->hdr->qoverflow_ofs);
          self->hdr->qoverflow_len = GUINT32_SWAP_LE_BE(self->hdr->qoverflow_len);
          self->hdr->qoverflow_count = GUINT32_SWAP_LE_BE(self->hdr->qoverflow_count);
          self->hdr->backlog_head = GUINT64_SWAP_LE_BE(self->hdr->backlog_head);
          self->hdr->backlog_len = GUINT64_SWAP_LE_BE(self->hdr->backlog_len);
          self->hdr->big_endian = (G_BYTE_ORDER == G_BIG_ENDIAN);
        }
      if (!_load_state(self, qout, qbacklog, qoverflow))
        {
          munmap((void *)self->hdr, sizeof(QDiskFileHeader));
          self->hdr = NULL;
          close(self->fd);
          self->fd = -1;
          return FALSE;
        }

    }
  return TRUE;
}
示例#5
0
int
main (int   argc,
      char *argv[])
{
  gchar *string;
  gushort gus;
  guint gui;
  gulong gul;
  gsize gsz;
  gshort gs;
  gint gi;
  glong gl;
  gint16 gi16t1;
  gint16 gi16t2;
  gint32 gi32t1;
  gint32 gi32t2;
  guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
  guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
  guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
	  gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
  gint64 gi64t1;
  gint64 gi64t2;
  gssize gsst1;
  gssize gsst2;
  gsize  gst1;
  gsize  gst2;

  #ifdef SYMBIAN
  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
  g_set_print_handler(mrtPrintHandler);
  #endif /*SYMBIAN*/
	  

  /* type sizes */
  g_assert (sizeof (gint8) == 1);
  g_assert (sizeof (gint16) == 2);
  g_assert (sizeof (gint32) == 4);
  g_assert (sizeof (gint64) == 8);

  g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
  g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
  g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);

  /* Test the G_(MIN|MAX|MAXU)(SHORT|INT|LONG) macros */

  gus = G_MAXUSHORT;
  gus++;
  g_assert (gus == 0);

  gui = G_MAXUINT;
  gui++;
  g_assert (gui == 0);

  gul = G_MAXULONG;
  gul++;
  g_assert (gul == 0);

  gsz = G_MAXSIZE;
  gsz++;
  
  g_assert (gsz == 0);

  gs = G_MAXSHORT;
  gs++;
  g_assert (gs == G_MINSHORT);

  gi = G_MAXINT;
  gi++;
  g_assert (gi == G_MININT);

  gl = G_MAXLONG;
  gl++;
  g_assert (gl == G_MINLONG);

  /* Test the G_G(U)?INT(16|32|64)_FORMAT macros */

  gi16t1 = -0x3AFA;
  gu16t1 = 0xFAFA;
  gi32t1 = -0x3AFAFAFA;
  gu32t1 = 0xFAFAFAFA; 

#define FORMAT "%" G_GINT16_FORMAT " %" G_GINT32_FORMAT \
               " %" G_GUINT16_FORMAT " %" G_GUINT32_FORMAT "\n"
  string = g_strdup_printf (FORMAT, gi16t1, gi32t1, gu16t1, gu32t1);
  sscanf (string, FORMAT, &gi16t2, &gi32t2, &gu16t2, &gu32t2);
  g_free (string);
  g_assert (gi16t1 == gi16t2);
  g_assert (gi32t1 == gi32t2);
  g_assert (gu16t1 == gu16t2);
  g_assert (gu32t1 == gu32t2);

  gi64t1 = G_GINT64_CONSTANT (-0x3AFAFAFAFAFAFAFA);
  gu64t1 = G_GINT64_CONSTANT (0xFAFAFAFAFAFAFAFA); 

#define FORMAT64 "%" G_GINT64_FORMAT " %" G_GUINT64_FORMAT "\n"
  string = g_strdup_printf (FORMAT64, gi64t1, gu64t1);
  sscanf (string, FORMAT64, &gi64t2, &gu64t2);
  g_free (string);
  g_assert (gi64t1 == gi64t2);
  g_assert (gu64t1 == gu64t2);

  gsst1 = -0x3AFAFAFA;
  gst1 = 0xFAFAFAFA; 

#define FORMATSIZE "%" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT "\n"
  string = g_strdup_printf (FORMATSIZE, gsst1, gst1);
  sscanf (string, FORMATSIZE, &gsst2, &gst2);
  g_free (string);
  g_assert (gsst1 == gsst2);
  g_assert (gst1 == gst2);
  
  #ifdef SYMBIAN
  testResultXml("type-test");
#endif /* EMULATOR */
  return 0;
}
示例#6
0
int 
main( int argc, char *argv[] )
{
  int i;
  
  // float 32
  WriteFile( "Float32.bin", float32_data, sizeof( float32_data ) );
  tFloat32 f32[sizeof( float32_data ) / sizeof( tFloat32 )];
  ReadFile( "Float32.bin", f32, sizeof( float32_data ) );
  
  if ( memcmp( float32_data, f32, sizeof( float32_data ) ) )
     {
       fprintf( stderr, "read/write float 32 error !\n" );
       return 1;
     }

  // float 64
  WriteFile( "Float64.bin", float64_data, sizeof( float64_data ) );
  tFloat64 f64[sizeof( float64_data ) / sizeof( tFloat64 )];
  ReadFile( "Float64.bin", f64, sizeof( float64_data ) );

  if ( memcmp( float64_data, f64, sizeof( float64_data ) ) )
     {
       fprintf( stderr, "read/write float 64 error !\n" );
       return 1;
     }

  // conversion
  if ( G_BYTE_ORDER == G_LITTLE_ENDIAN )
       ReadFile( "Float32.bin.ppc", f32, sizeof( float32_data ) );
  else
       ReadFile( "Float32.bin.i386", f32, sizeof( float32_data ) );

  char *p = (char *)f32;

  for( i = 0; i < float32_data_num; i++, p += sizeof( tFloat32 ) )
     {
	/* compile error */
//       unsigned int v = *(unsigned int *)p;
       unsigned int v = *(unsigned int *)(void *)p;
       v = GUINT32_SWAP_LE_BE( v );
	/* compile error */
//       *(unsigned int *)p = v;
       *(unsigned int *)(void *)p = v;
     }

  if ( memcmp( float32_data, f32, sizeof( float32_data ) ) )
     {
       fprintf( stderr, "byteswap float 32 fail !\n" );
       return 1;
     }

  // conversion
  if ( G_BYTE_ORDER == G_LITTLE_ENDIAN )
       ReadFile( "Float64.bin.ppc", f64, sizeof( float64_data ) );
  else
       ReadFile( "Float64.bin.i386", f64, sizeof( float64_data ) );

  p = (char *)f64;

  for( i = 0; i < float64_data_num; i++, p += sizeof( tFloat64 ) )
     {
	/* compile error */
//       unsigned long long v = *(unsigned long long *)p;
       unsigned long long v = *(unsigned long long *)(void *)p;
       v = GUINT64_SWAP_LE_BE( v );
	/* compile error */
//       *(unsigned long long *)p = v;
       *(unsigned long long *)(void *)p = v;
     }

  if ( memcmp( float64_data, f64, sizeof( float64_data ) ) )
     {
       fprintf( stderr, "byteswap float 64 fail !\n" );
       return 1;
     }

  return 0;
}
gboolean
log_proto_buffered_server_restart_with_state(LogProtoServer *s, PersistState *persist_state, const gchar *persist_name)
{
  LogProtoBufferedServer *self = (LogProtoBufferedServer *) s;
  guint8 persist_version;
  PersistEntryHandle old_state_handle;
  gpointer old_state;
  gsize old_state_size;
  PersistEntryHandle new_state_handle = 0;
  gpointer new_state = NULL;
  gboolean success;

  self->pos_tracking = TRUE;
  self->persist_state = persist_state;
  old_state_handle = persist_state_lookup_entry(persist_state, persist_name, &old_state_size, &persist_version);
  if (!old_state_handle)
    {
      new_state_handle = log_proto_buffered_server_alloc_state(self, persist_state, persist_name);
      if (!new_state_handle)
        goto fallback_non_persistent;
      log_proto_buffered_server_apply_state(self, new_state_handle, persist_name);
      return TRUE;
    }
  if (persist_version < 4)
    {
      new_state_handle = log_proto_buffered_server_alloc_state(self, persist_state, persist_name);
      if (!new_state_handle)
        goto fallback_non_persistent;

      old_state = persist_state_map_entry(persist_state, old_state_handle);
      new_state = persist_state_map_entry(persist_state, new_state_handle);
      success = log_proto_buffered_server_convert_state(self, persist_version, old_state, old_state_size, new_state);
      persist_state_unmap_entry(persist_state, old_state_handle);
      persist_state_unmap_entry(persist_state, new_state_handle);

      /* we're using the newly allocated state structure regardless if
       * conversion succeeded. If the conversion went wrong then
       * new_state is still in its freshly initialized form since the
       * convert function will not touch the state in the error
       * branches.
       */

      log_proto_buffered_server_apply_state(self, new_state_handle, persist_name);
      return success;
    }
  else if (persist_version == 4)
    {
      LogProtoBufferedServerState *state;

      old_state = persist_state_map_entry(persist_state, old_state_handle);
      state = old_state;
      if ((state->header.big_endian && G_BYTE_ORDER == G_LITTLE_ENDIAN) ||
          (!state->header.big_endian && G_BYTE_ORDER == G_BIG_ENDIAN))
        {

          /* byte order conversion in order to avoid the hassle with
             scattered byte order conversions in the code */

          state->header.big_endian = !state->header.big_endian;
          state->buffer_pos = GUINT32_SWAP_LE_BE(state->buffer_pos);
          state->pending_buffer_pos = GUINT32_SWAP_LE_BE(state->pending_buffer_pos);
          state->pending_buffer_end = GUINT32_SWAP_LE_BE(state->pending_buffer_end);
          state->buffer_size = GUINT32_SWAP_LE_BE(state->buffer_size);
          state->raw_stream_pos = GUINT64_SWAP_LE_BE(state->raw_stream_pos);
          state->raw_buffer_size = GUINT32_SWAP_LE_BE(state->raw_buffer_size);
          state->pending_raw_stream_pos = GUINT64_SWAP_LE_BE(state->pending_raw_stream_pos);
          state->pending_raw_buffer_size = GUINT32_SWAP_LE_BE(state->pending_raw_buffer_size);
          state->file_size = GUINT64_SWAP_LE_BE(state->file_size);
          state->file_inode = GUINT64_SWAP_LE_BE(state->file_inode);
        }

      if (state->header.version > 0)
        {
          msg_error("Internal error restoring log reader state, stored data is too new",
                    evt_tag_int("version", state->header.version));
          goto error;
        }
      persist_state_unmap_entry(persist_state, old_state_handle);
      log_proto_buffered_server_apply_state(self, old_state_handle, persist_name);
      return TRUE;
    }
  else
    {
      msg_error("Internal error restoring log reader state, stored data is too new",
                evt_tag_int("version", persist_version));
      goto error;
    }
  return TRUE;
 fallback_non_persistent:
  new_state = g_new0(LogProtoBufferedServerState, 1);
 error:
  if (!new_state)
    {
      new_state_handle = log_proto_buffered_server_alloc_state(self, persist_state, persist_name);
      if (!new_state_handle)
        goto fallback_non_persistent;
      new_state = persist_state_map_entry(persist_state, new_state_handle);
    }
  if (new_state)
    {
      LogProtoBufferedServerState *state = new_state;

      /* error happened,  restart the file from the beginning */
      state->raw_stream_pos = 0;
      state->file_inode = 0;
      state->file_size = 0;
      if (new_state_handle)
        log_proto_buffered_server_apply_state(self, new_state_handle, persist_name);
      else
        self->state1 = new_state;
    }
  if (new_state_handle)
    {
      persist_state_unmap_entry(persist_state, new_state_handle);
    }
  return FALSE;
}
示例#8
0
int
DemarshalSimpleTypes( int byte_order, tMarshalType type,
                      void *data, const void *buffer )
{
  switch( type )
     {
       case eMtVoid:
	    return 0;

       case eMtUint8:
       case eMtInt8:
            {
              tUint8 v = *(const tUint8 *)buffer;
              *(tUint8 *)data = v;
            }

            return sizeof( tUint8 );

       case eMtInt16:
       case eMtUint16:
            {
              tUint16 v;
              memcpy( &v, buffer, sizeof( tUint16 ) );

              if ( G_BYTE_ORDER != byte_order )
                   v = GUINT16_SWAP_LE_BE( v );
              
              *(tUint16 *)data = v;
            }            

            return sizeof( tUint16 );

       case eMtUint32:
       case eMtInt32:
            {
              tUint32 v;
              memcpy( &v, buffer, sizeof( tUint32 ) );

              if ( G_BYTE_ORDER != byte_order )
                   v = GUINT32_SWAP_LE_BE( v );

              *(tUint32 *)data = v;
            }

            return sizeof( tUint32 );

       case eMtUint64:
       case eMtInt64:
            {
              tUint64 v;
              memcpy( &v, buffer, sizeof( tUint64 ) );

              if ( G_BYTE_ORDER != byte_order )
                   v = GUINT64_SWAP_LE_BE( v );

              *(tUint64 *)data = v;
            }

            return sizeof( tUint64 );

       case eMtFloat32:
            {
              // this has been tested for i386 and PPC
              tFloat32Uint32 v;
              memcpy( &(v.m_f32), buffer, sizeof( tFloat32 ) );

              if ( G_BYTE_ORDER != byte_order )
                   v.m_u32 = GUINT32_SWAP_LE_BE( v.m_u32 );

              *(tFloat32 *)data = v.m_f32;
            }

            return sizeof( tFloat32 );

       case eMtFloat64:
            {
              // this has been tested for i386 and PPC
              tFloat64Uint64 v;
              memcpy( &(v.m_f64), buffer, sizeof( tFloat64 ) );

              if ( G_BYTE_ORDER != byte_order )
                   v.m_u64 = GUINT64_SWAP_LE_BE( v.m_u64 );

              *(tFloat64 *)data = v.m_f64;
            }

            return sizeof( tFloat64 );

       default:
            break;
     }

  assert( 0 );
  return -1;
}
示例#9
0
int
main (int   argc,
      char *argv[])
{
  GList *list, *t;
  GSList *slist, *st;
  GHashTable *hash_table;
  GMemChunk *mem_chunk;
  GStringChunk *string_chunk;
  GTimer *timer;
  gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
  gchar *string;

  gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
  gint i, j;
  GArray *garray;
  GPtrArray *gparray;
  GByteArray *gbarray;
  GString *string1, *string2;
  GTree *tree;
  char chars[62];
  GRelation *relation;
  GTuples *tuples;
  gint data [1024];
  struct {
    gchar *filename;
    gchar *dirname;
  } dirname_checks[] = {
#ifndef NATIVE_WIN32
    { "/", "/" },
    { "////", "/" },
    { ".////", "." },
    { ".", "." },
    { "..", "." },
    { "../", ".." },
    { "..////", ".." },
    { "", "." },
    { "a/b", "a" },
    { "a/b/", "a/b" },
    { "c///", "c" },
#else
    { "\\", "\\" },
    { ".\\\\\\\\", "." },
    { ".", "." },
    { "..", "." },
    { "..\\", ".." },
    { "..\\\\\\\\", ".." },
    { "", "." },
    { "a\\b", "a" },
    { "a\\b\\", "a\\b" },
    { "c\\\\\\", "c" },
#endif
  };
  guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
  guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
  guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
#ifdef G_HAVE_GINT64
  guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
	  gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
#endif

  g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
	   glib_major_version,
	   glib_minor_version,
	   glib_micro_version,
	   glib_interface_age,
	   glib_binary_age);

  string = g_get_current_dir ();
  g_print ("cwd: %s\n", string);
  g_free (string);
  g_print ("user: %s\n", g_get_user_name ());
  g_print ("real: %s\n", g_get_real_name ());
  g_print ("home: %s\n", g_get_home_dir ());
  g_print ("tmp-dir: %s\n", g_get_tmp_dir ());

  /* type sizes */
  g_print ("checking size of gint8: %d", (int)sizeof (gint8));
  TEST (NULL, sizeof (gint8) == 1);
  g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
  TEST (NULL, sizeof (gint16) == 2);
  g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
  TEST (NULL, sizeof (gint32) == 4);
#ifdef	G_HAVE_GINT64
  g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
  TEST (NULL, sizeof (gint64) == 8);
#endif	/* G_HAVE_GINT64 */
  g_print ("\n");

  g_print ("checking g_dirname()...");
  for (i = 0; i < n_dirname_checks; i++)
    {
      gchar *dirname;

      dirname = g_dirname (dirname_checks[i].filename);
      if (strcmp (dirname, dirname_checks[i].dirname) != 0)
	{
	  g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
		   dirname_checks[i].filename,
		   dirname_checks[i].dirname,
		   dirname);
	  n_dirname_checks = 0;
	}
      g_free (dirname);
    }
  if (n_dirname_checks)
    g_print ("ok\n");

  g_print ("checking doubly linked lists...");

  list = NULL;
  for (i = 0; i < 10; i++)
    list = g_list_append (list, &nums[i]);
  list = g_list_reverse (list);

  for (i = 0; i < 10; i++)
    {
      t = g_list_nth (list, i);
      if (*((gint*) t->data) != (9 - i))
	g_error ("Regular insert failed");
    }

  for (i = 0; i < 10; i++)
    if(g_list_position(list, g_list_nth (list, i)) != i)
      g_error("g_list_position does not seem to be the inverse of g_list_nth\n");

  g_list_free (list);
  list = NULL;

  for (i = 0; i < 10; i++)
    list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);

  /*
  g_print("\n");
  g_list_foreach (list, my_list_print, NULL);
  */

  for (i = 0; i < 10; i++)
    {
      t = g_list_nth (list, i);
      if (*((gint*) t->data) != i)
         g_error ("Sorted insert failed");
    }

  g_list_free (list);
  list = NULL;

  for (i = 0; i < 10; i++)
    list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);

  /*
  g_print("\n");
  g_list_foreach (list, my_list_print, NULL);
  */

  for (i = 0; i < 10; i++)
    {
      t = g_list_nth (list, i);
      if (*((gint*) t->data) != (9 - i))
         g_error ("Sorted insert failed");
    }

  g_list_free (list);
  list = NULL;

  for (i = 0; i < 10; i++)
    list = g_list_prepend (list, &morenums[i]);

  list = g_list_sort (list, my_list_compare_two);

  /*
  g_print("\n");
  g_list_foreach (list, my_list_print, NULL);
  */

  for (i = 0; i < 10; i++)
    {
      t = g_list_nth (list, i);
      if (*((gint*) t->data) != (9 - i))
         g_error ("Merge sort failed");
    }

  g_list_free (list);

  g_print ("ok\n");


  g_print ("checking singly linked lists...");

  slist = NULL;
  for (i = 0; i < 10; i++)
    slist = g_slist_append (slist, &nums[i]);
  slist = g_slist_reverse (slist);

  for (i = 0; i < 10; i++)
    {
      st = g_slist_nth (slist, i);
      if (*((gint*) st->data) != (9 - i))
	g_error ("failed");
    }

  g_slist_free (slist);
  slist = NULL;

  for (i = 0; i < 10; i++)
    slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);

  /*
  g_print("\n");
  g_slist_foreach (slist, my_list_print, NULL);
  */

  for (i = 0; i < 10; i++)
    {
      st = g_slist_nth (slist, i);
      if (*((gint*) st->data) != i)
         g_error ("Sorted insert failed");
    }

  g_slist_free(slist);
  slist = NULL;

  for (i = 0; i < 10; i++)
    slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);

  /*
  g_print("\n");
  g_slist_foreach (slist, my_list_print, NULL);
  */

  for (i = 0; i < 10; i++)
    {
      st = g_slist_nth (slist, i);
      if (*((gint*) st->data) != (9 - i))
         g_error("Sorted insert failed");
    }

  g_slist_free(slist);
  slist = NULL;

  for (i = 0; i < 10; i++)
    slist = g_slist_prepend (slist, &morenums[i]);

  slist = g_slist_sort (slist, my_list_compare_two);

  /*
  g_print("\n");
  g_slist_foreach (slist, my_list_print, NULL);
  */

  for (i = 0; i < 10; i++)
    {
      st = g_slist_nth (slist, i);
      if (*((gint*) st->data) != (9 - i))
         g_error("Sorted insert failed");
    }

  g_slist_free(slist);

  g_print ("ok\n");


  g_print ("checking binary trees...\n");

  tree = g_tree_new (my_compare);
  i = 0;
  for (j = 0; j < 10; j++, i++)
    {
      chars[i] = '0' + j;
      g_tree_insert (tree, &chars[i], &chars[i]);
    }
  for (j = 0; j < 26; j++, i++)
    {
      chars[i] = 'A' + j;
      g_tree_insert (tree, &chars[i], &chars[i]);
    }
  for (j = 0; j < 26; j++, i++)
    {
      chars[i] = 'a' + j;
      g_tree_insert (tree, &chars[i], &chars[i]);
    }

  g_print ("tree height: %d\n", g_tree_height (tree));
  g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));

  g_print ("tree: ");
  g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
  g_print ("\n");

  for (i = 0; i < 10; i++)
    g_tree_remove (tree, &chars[i]);

  g_print ("tree height: %d\n", g_tree_height (tree));
  g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));

  g_print ("tree: ");
  g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
  g_print ("\n");

  g_print ("ok\n");


  /* check n-way trees */
  g_node_test ();

  g_print ("checking mem chunks...");

  mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);

  for (i = 0; i < 10000; i++)
    {
      mem[i] = g_chunk_new (gchar, mem_chunk);

      for (j = 0; j < 50; j++)
	mem[i][j] = i * j;
    }

  for (i = 0; i < 10000; i++)
    {
      g_mem_chunk_free (mem_chunk, mem[i]);
    }

  g_print ("ok\n");


  g_print ("checking hash tables...");

  hash_table = g_hash_table_new (my_hash, my_hash_compare);
  for (i = 0; i < 10000; i++)
    {
      array[i] = i;
      g_hash_table_insert (hash_table, &array[i], &array[i]);
    }
  g_hash_table_foreach (hash_table, my_hash_callback, NULL);

  for (i = 0; i < 10000; i++)
    if (array[i] == 0)
      g_print ("%d\n", i);

  for (i = 0; i < 10000; i++)
    g_hash_table_remove (hash_table, &array[i]);

  for (i = 0; i < 10000; i++)
    {
      array[i] = i;
      g_hash_table_insert (hash_table, &array[i], &array[i]);
    }

  if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
      g_hash_table_size (hash_table) != 5000)
    g_print ("bad!\n");

  g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);


  g_hash_table_destroy (hash_table);

  g_print ("ok\n");


  g_print ("checking string chunks...");

  string_chunk = g_string_chunk_new (1024);

  for (i = 0; i < 100000; i ++)
    {
      tmp_string = g_string_chunk_insert (string_chunk, "hi pete");

      if (strcmp ("hi pete", tmp_string) != 0)
	g_error ("string chunks are broken.\n");
    }

  tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);

  g_assert (tmp_string_2 != tmp_string &&
	    strcmp(tmp_string_2, tmp_string) == 0);

  tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);

  g_assert (tmp_string_2 == tmp_string);

  g_string_chunk_free (string_chunk);

  g_print ("ok\n");


  g_print ("checking arrays...");

  garray = g_array_new (FALSE, FALSE, sizeof (gint));
  for (i = 0; i < 10000; i++)
    g_array_append_val (garray, i);

  for (i = 0; i < 10000; i++)
    if (g_array_index (garray, gint, i) != i)
      g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);

  g_array_free (garray, TRUE);

  garray = g_array_new (FALSE, FALSE, sizeof (gint));
  for (i = 0; i < 100; i++)
    g_array_prepend_val (garray, i);

  for (i = 0; i < 100; i++)
    if (g_array_index (garray, gint, i) != (100 - i - 1))
      g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);

  g_array_free (garray, TRUE);

  g_print ("ok\n");


  g_print ("checking strings...");

  string1 = g_string_new ("hi pete!");
  string2 = g_string_new ("");

  g_assert (strcmp ("hi pete!", string1->str) == 0);

  for (i = 0; i < 10000; i++)
    g_string_append_c (string1, 'a'+(i%26));

#if !(defined (_MSC_VER) || defined (__LCC__))
  /* MSVC and LCC use the same run-time C library, which doesn't like
     the %10000.10000f format... */
  g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
		    "this pete guy sure is a wuss, like he's the number ",
		    1,
		    " wuss.  everyone agrees.\n",
		    string1->str,
		    10, 666, 15, 15, 666.666666666, 666.666666666);
#else
  g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
		    "this pete guy sure is a wuss, like he's the number ",
		    1,
		    " wuss.  everyone agrees.\n",
		    string1->str,
		    10, 666, 15, 15, 666.666666666, 666.666666666);
#endif

  g_print ("string2 length = %d...\n", string2->len);
  string2->str[70] = '\0';
  g_print ("first 70 chars:\n%s\n", string2->str);
  string2->str[141] = '\0';
  g_print ("next 70 chars:\n%s\n", string2->str+71);
  string2->str[212] = '\0';
  g_print ("and next 70:\n%s\n", string2->str+142);
  g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);

  g_print ("ok\n");

  g_print ("checking timers...\n");

  timer = g_timer_new ();
  g_print ("  spinning for 3 seconds...\n");

  g_timer_start (timer);
  while (g_timer_elapsed (timer, NULL) < 3)
    ;

  g_timer_stop (timer);
  g_timer_destroy (timer);

  g_print ("ok\n");

  g_print ("checking g_strcasecmp...");
  g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
  g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
  g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
  g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
  g_assert (g_strcasecmp ("", "") == 0);
  g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
  g_assert (g_strcasecmp ("a", "b") < 0);
  g_assert (g_strcasecmp ("a", "B") < 0);
  g_assert (g_strcasecmp ("A", "b") < 0);
  g_assert (g_strcasecmp ("A", "B") < 0);
  g_assert (g_strcasecmp ("b", "a") > 0);
  g_assert (g_strcasecmp ("b", "A") > 0);
  g_assert (g_strcasecmp ("B", "a") > 0);
  g_assert (g_strcasecmp ("B", "A") > 0);

  g_print ("ok\n");

  g_print ("checking g_strdup...");
  g_assert(g_strdup(NULL) == NULL);
  string = g_strdup(GLIB_TEST_STRING);
  g_assert(string != NULL);
  g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
  g_free(string);

  g_print ("ok\n");

  g_print ("checking g_strconcat...");
  string = g_strconcat(GLIB_TEST_STRING, NULL);
  g_assert(string != NULL);
  g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
  g_free(string);
  string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
  		       GLIB_TEST_STRING, NULL);
  g_assert(string != NULL);
  g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
  			  GLIB_TEST_STRING) == 0);
  g_free(string);

  g_print ("ok\n");

  g_print ("checking g_strdup_printf...");
  string = g_strdup_printf ("%05d %-5s", 21, "test");
  g_assert (string != NULL);
  g_assert (strcmp(string, "00021 test ") == 0);
  g_free (string);

  g_print ("ok\n");

  /* g_debug (argv[0]); */

  /* Relation tests */

  g_print ("checking relations...");

  relation = g_relation_new (2);

  g_relation_index (relation, 0, g_int_hash, g_int_equal);
  g_relation_index (relation, 1, g_int_hash, g_int_equal);

  for (i = 0; i < 1024; i += 1)
    data[i] = i;

  for (i = 1; i < 1023; i += 1)
    {
      g_relation_insert (relation, data + i, data + i + 1);
      g_relation_insert (relation, data + i, data + i - 1);
    }

  for (i = 2; i < 1022; i += 1)
    {
      g_assert (! g_relation_exists (relation, data + i, data + i));
      g_assert (! g_relation_exists (relation, data + i, data + i + 2));
      g_assert (! g_relation_exists (relation, data + i, data + i - 2));
    }

  for (i = 1; i < 1023; i += 1)
    {
      g_assert (g_relation_exists (relation, data + i, data + i + 1));
      g_assert (g_relation_exists (relation, data + i, data + i - 1));
    }

  for (i = 2; i < 1022; i += 1)
    {
      g_assert (g_relation_count (relation, data + i, 0) == 2);
      g_assert (g_relation_count (relation, data + i, 1) == 2);
    }

  g_assert (g_relation_count (relation, data, 0) == 0);

  g_assert (g_relation_count (relation, data + 42, 0) == 2);
  g_assert (g_relation_count (relation, data + 43, 1) == 2);
  g_assert (g_relation_count (relation, data + 41, 1) == 2);
  g_relation_delete (relation, data + 42, 0);
  g_assert (g_relation_count (relation, data + 42, 0) == 0);
  g_assert (g_relation_count (relation, data + 43, 1) == 1);
  g_assert (g_relation_count (relation, data + 41, 1) == 1);

  tuples = g_relation_select (relation, data + 200, 0);

  g_assert (tuples->len == 2);

#if 0
  for (i = 0; i < tuples->len; i += 1)
    {
      printf ("%d %d\n",
	      *(gint*) g_tuples_index (tuples, i, 0),
	      *(gint*) g_tuples_index (tuples, i, 1));
    }
#endif

  g_assert (g_relation_exists (relation, data + 300, data + 301));
  g_relation_delete (relation, data + 300, 0);
  g_assert (!g_relation_exists (relation, data + 300, data + 301));

  g_tuples_destroy (tuples);

  g_relation_destroy (relation);

  relation = NULL;

  g_print ("ok\n");

  g_print ("checking pointer arrays...");

  gparray = g_ptr_array_new ();
  for (i = 0; i < 10000; i++)
    g_ptr_array_add (gparray, GINT_TO_POINTER (i));

  for (i = 0; i < 10000; i++)
    if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
      g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));

  g_ptr_array_free (gparray, TRUE);

  g_print ("ok\n");


  g_print ("checking byte arrays...");

  gbarray = g_byte_array_new ();
  for (i = 0; i < 10000; i++)
    g_byte_array_append (gbarray, (guint8*) "abcd", 4);

  for (i = 0; i < 10000; i++)
    {
      g_assert (gbarray->data[4*i] == 'a');
      g_assert (gbarray->data[4*i+1] == 'b');
      g_assert (gbarray->data[4*i+2] == 'c');
      g_assert (gbarray->data[4*i+3] == 'd');
    }

  g_byte_array_free (gbarray, TRUE);
  g_print ("ok\n");

  g_printerr ("g_log tests:");
  g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
  g_message ("the next warning is a test:");
  string = NULL;
  g_print (string);

  g_print ("checking endian macros (host is ");
#if G_BYTE_ORDER == G_BIG_ENDIAN
  g_print ("big endian)...");
#else
  g_print ("little endian)...");
#endif
  g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
  g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
#ifdef G_HAVE_GINT64
  g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
#endif
  g_print ("ok\n");

  return 0;
}
示例#10
0
static int
DemarshalSimpleTypes( int byte_order, tMarshalType type, void *data, const void *buffer )
{
  union
  {
    tInt16    i16;
    tUint16   ui16;
    tInt32    i32;
    tUint32   ui32;
    tInt64    i64;
    tUint64   ui64;
    tFloat32  f32;
    tFloat64  f64;
  } u;

  // NB Glib does not provide SWAP_LE_BE macro for signed types!

  switch( type )
     {
       case eMtVoid:
	    return 0;

       case eMtInt8:
	    memcpy(data, buffer, sizeof(tInt8));
	    return sizeof(tInt8);

       case eMtUint8:
	    memcpy(data, buffer, sizeof(tUint8));
	    return sizeof(tUint8);

       case eMtInt16:
	    memcpy(&u.i16, buffer, sizeof(tInt16));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui16 = GUINT16_SWAP_LE_BE( u.ui16 );
	       }
	    memcpy(data, &u.i16, sizeof(tInt16));
	    return sizeof(tInt16);

       case eMtUint16:
	    memcpy(&u.ui16, buffer, sizeof(tUint16));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui16 = GUINT16_SWAP_LE_BE( u.ui16 );
	       }
	    memcpy(data, &u.ui16, sizeof(tUint16));
	    return sizeof(tUint16);

       case eMtInt32:
	    memcpy(&u.i32, buffer, sizeof(tInt32));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui32 = GUINT32_SWAP_LE_BE( u.ui32 );
	       }
	    memcpy(data, &u.i32, sizeof(tInt32));
	    return sizeof(tInt32);
	
       case eMtUint32:
	    memcpy(&u.ui32, buffer, sizeof(tUint32));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui32 = GUINT32_SWAP_LE_BE( u.ui32 );
	       }
	    memcpy(data, &u.ui32, sizeof(tUint32));
	    return sizeof(tUint32);

       case eMtInt64:
	    memcpy(&u.i64, buffer, sizeof(tInt64));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui64 = GUINT64_SWAP_LE_BE( u.ui64 );
	       }
	    memcpy(data, &u.i64, sizeof(tInt64));
	    return sizeof(tInt64);
	
       case eMtUint64:
	    memcpy(&u.ui64, buffer, sizeof(tUint64));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui64 = GUINT64_SWAP_LE_BE( u.ui64 );
	       }
	    memcpy(data, &u.ui64, sizeof(tUint64));
	    return sizeof(tUint64);
	
       case eMtFloat32:
	
	    memcpy(&u.f32, buffer, sizeof(tFloat32));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui32 = GUINT32_SWAP_LE_BE( u.ui32 );
	       }
	    memcpy(data, &u.f32, sizeof(tFloat32));
	    return sizeof(tFloat32);
	
       case eMtFloat64:
	    memcpy(&u.f64, buffer, sizeof(tFloat64));
	    if ( G_BYTE_ORDER != byte_order )
	       {
	         u.ui64 = GUINT64_SWAP_LE_BE( u.ui64 );
	       }
	    memcpy(data, &u.f64, sizeof(tFloat64));
	    return sizeof(tFloat64);
	
       default:
            CRIT( "Unknown marshal type %d!", type );
            return -ENOSYS;
     }
}