github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/tests/endian_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"log"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/ncruces/go-sqlite3"
    11  )
    12  
    13  func Test_endianness(t *testing.T) {
    14  	big := binary.BigEndian.AppendUint64(nil, 0x1234567890ABCDEF)
    15  	little := binary.LittleEndian.AppendUint64(nil, 0x1234567890ABCDEF)
    16  	native := binary.NativeEndian.AppendUint64(nil, 0x1234567890ABCDEF)
    17  	switch {
    18  	case bytes.Equal(big, native):
    19  		t.Log("Platform is big endian")
    20  	case bytes.Equal(little, native):
    21  		t.Log("Platform is little endian")
    22  	default:
    23  		t.Fatal("Platform is middle endian")
    24  	}
    25  
    26  	db, err := sqlite3.Open(":memory:")
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  	defer db.Close()
    31  
    32  	err = db.Exec(`CREATE TABLE test (col)`)
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  
    37  	const value int64 = -9223372036854775808
    38  	{
    39  		stmt, _, err := db.Prepare(`INSERT INTO test VALUES (?)`)
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  		defer stmt.Close()
    44  
    45  		err = stmt.BindInt64(1, value)
    46  		if err != nil {
    47  			t.Fatal(err)
    48  		}
    49  
    50  		err = stmt.Exec()
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  	}
    55  	{
    56  		stmt, _, err := db.Prepare(`SELECT * FROM test`)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		defer stmt.Close()
    61  
    62  		if stmt.Step() {
    63  			if got := stmt.ColumnInt64(0); got != value {
    64  				t.Errorf("got %d, want %d", got, value)
    65  			}
    66  			if got := stmt.ColumnText(0); got != strconv.FormatInt(value, 10) {
    67  				t.Errorf("got %s, want %d", got, value)
    68  			}
    69  		}
    70  		if err != nil {
    71  			t.Fatal(err)
    72  		}
    73  	}
    74  
    75  }