github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/misc/cgo/test/align.go (about)

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cgotest
     6  
     7  /*
     8  #include <stdio.h>
     9  
    10  typedef unsigned char Uint8;
    11  typedef unsigned short Uint16;
    12  
    13  typedef enum {
    14   MOD1 = 0x0000,
    15   MODX = 0x8000
    16  } SDLMod;
    17  
    18  typedef enum {
    19   A = 1,
    20   B = 322,
    21   SDLK_LAST
    22  } SDLKey;
    23  
    24  typedef struct SDL_keysym {
    25  	Uint8 scancode;
    26  	SDLKey sym;
    27  	SDLMod mod;
    28  	Uint16 unicode;
    29  } SDL_keysym;
    30  
    31  typedef struct SDL_KeyboardEvent {
    32  	Uint8 typ;
    33  	Uint8 which;
    34  	Uint8 state;
    35  	SDL_keysym keysym;
    36  } SDL_KeyboardEvent;
    37  
    38  void makeEvent(SDL_KeyboardEvent *event) {
    39   unsigned char *p;
    40   int i;
    41  
    42   p = (unsigned char*)event;
    43   for (i=0; i<sizeof *event; i++) {
    44     p[i] = i;
    45   }
    46  }
    47  
    48  int same(SDL_KeyboardEvent* e, Uint8 typ, Uint8 which, Uint8 state, Uint8 scan, SDLKey sym, SDLMod mod, Uint16 uni) {
    49    return e->typ == typ && e->which == which && e->state == state && e->keysym.scancode == scan && e->keysym.sym == sym && e->keysym.mod == mod && e->keysym.unicode == uni;
    50  }
    51  
    52  void cTest(SDL_KeyboardEvent *event) {
    53   printf("C: %#x %#x %#x %#x %#x %#x %#x\n", event->typ, event->which, event->state,
    54     event->keysym.scancode, event->keysym.sym, event->keysym.mod, event->keysym.unicode);
    55   fflush(stdout);
    56  }
    57  
    58  */
    59  import "C"
    60  
    61  import (
    62  	"testing"
    63  )
    64  
    65  func testAlign(t *testing.T) {
    66  	var evt C.SDL_KeyboardEvent
    67  	C.makeEvent(&evt)
    68  	if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 {
    69  		t.Error("*** bad alignment")
    70  		C.cTest(&evt)
    71  		t.Errorf("Go: %#x %#x %#x %#x %#x %#x %#x\n",
    72  			evt.typ, evt.which, evt.state, evt.keysym.scancode,
    73  			evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode)
    74  		t.Error(evt)
    75  	}
    76  }