github.com/gagliardetto/solana-go@v1.11.0/programs/serum/init_test.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  // This file has been modified by github.com/gagliardetto
     3  //
     4  // Copyright 2020 dfuse Platform Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package serum
    19  
    20  import (
    21  	"encoding/hex"
    22  	"io/ioutil"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/klauspost/compress/zstd"
    27  	"github.com/streamingfast/logging"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func init() {
    32  	logging.TestingOverride()
    33  }
    34  
    35  func writeCompressedFile(t *testing.T, filename string, data []byte) {
    36  	writeFile(t, filename, zstEncoder.EncodeAll(data, nil))
    37  }
    38  
    39  func readCompressedFile(t *testing.T, file string) []byte {
    40  	data := readFile(t, file)
    41  
    42  	out, err := zstDecoder.DecodeAll(data, nil)
    43  	require.NoError(t, err)
    44  
    45  	return out
    46  }
    47  
    48  func readHexFile(t *testing.T, file string) []byte {
    49  	data := readFile(t, file)
    50  
    51  	out, err := hex.DecodeString(string(data))
    52  	require.NoError(t, err)
    53  
    54  	return out
    55  }
    56  
    57  var zstEncoder, _ = zstd.NewWriter(nil)
    58  var zstDecoder, _ = zstd.NewReader(nil)
    59  
    60  func readFile(t *testing.T, file string) []byte {
    61  	data, err := ioutil.ReadFile(file)
    62  	require.NoError(t, err)
    63  
    64  	return data
    65  }
    66  
    67  func writeFile(t *testing.T, filename string, data []byte) {
    68  	require.NoError(t, ioutil.WriteFile(filename, data, os.ModePerm), "unable to write file %s", filename)
    69  }
    70  
    71  func writeJSONFile(t *testing.T, filename string, v interface{}) {
    72  	out, err := json.MarshalIndent(v, "", "  ")
    73  	require.NoError(t, err)
    74  
    75  	require.NoError(t, ioutil.WriteFile(filename, out, os.ModePerm), "unable to write JSON file %s", filename)
    76  }
    77  
    78  func readJSONFile(t *testing.T, file string, v interface{}) {
    79  	data, err := ioutil.ReadFile(file)
    80  	require.NoError(t, err)
    81  
    82  	require.NoError(t, json.Unmarshal(data, v))
    83  	return
    84  }