Skip to content

Commit

Permalink
Add test and fix found issues
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Dec 24, 2024
1 parent c9384e3 commit 33d2378
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 8 deletions.
19 changes: 11 additions & 8 deletions itch/5.0/stock_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func (s StockDirectory) Bytes() []byte {
binary.BigEndian.PutUint64(data[3:11], uint64(s.Timestamp.Nanoseconds()))
binary.BigEndian.PutUint16(data[3:5], s.TrackingNumber)

copy(data[11:19], []byte(fmt.Sprintf("'%-8s'", s.Stock)))
copy(data[11:19], []byte(fmt.Sprintf("%-8s", s.Stock)))

data[19] = byte(s.MarketCategory)
data[20] = byte(s.FinancialStatusIndicator)

binary.BigEndian.AppendUint32(data[21:25], s.RoundLotSize)
binary.BigEndian.PutUint32(data[21:25], s.RoundLotSize)

if s.RoundLotsOnly {
data[25] = 'Y'
Expand All @@ -175,14 +175,12 @@ func (s StockDirectory) Bytes() []byte {
}

data[26] = byte(s.IssueClassification)
copy(data[27:29], []byte(s.IssueSubType))
underlying := string(s.IssueSubType)
copy(data[27:29], []byte(fmt.Sprintf("%-2s", underlying)))

if s.Authenticity == AUTHENTICITY_LIVE {
data[29] = 'P'
} else {
data[29] = 'T'
}
data[29] = byte(s.Authenticity)

// TODO: Make types
data[30] = []byte(s.ShortSaleThresholdIndicator)[0]
data[31] = []byte(s.IpoFlag)[0]
data[32] = []byte(s.LuldReferencePriceTier)[0]
Expand All @@ -204,6 +202,11 @@ func ParseStockDirectory(data []byte) (StockDirectory, error) {
return StockDirectory{}, NewInvalidPacketSize(stockDirectorySize, len(data))
}

for i := range len(data) {
fmt.Printf("%v,", data[i])
}
fmt.Println("")

locate := binary.BigEndian.Uint16(data[1:3])
tracking := binary.BigEndian.Uint16(data[3:5])
data[3] = 0
Expand Down
104 changes: 104 additions & 0 deletions itch/5.0/stock_directory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2022 Mark Edward Winter
*/

package itch

import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestParseStockDirectory(t *testing.T) {
timestamp, _ := time.ParseDuration("3h7m14.939262551s")

type args struct {
data []byte
}
tests := []struct {
name string
args args
want StockDirectory
wantErr bool
}{
{
name: "",
args: args{data: []byte{82, 3, 81, 0, 0, 10, 55, 214, 144, 86, 87, 66, 72, 66, 32, 32, 32, 32, 32, 65, 32, 0, 0, 0, 100, 78, 67, 90, 32, 80, 78, 32, 50, 78, 0, 0, 0, 0, 78}},
want: StockDirectory{
Timestamp: timestamp,
Stock: "BHB",
StockLocate: 849,
TrackingNumber: 0,
RoundLotSize: 100,
RoundLotsOnly: false,
IssueSubType: ICS_NOT_APPLICABLE,
IssueClassification: IC_COMMON_STOCK,
InverseIndicator: false,
Authenticity: AUTHENTICITY_LIVE,
EtpLeverageFactor: 0,
ShortSaleThresholdIndicator: "N",
IpoFlag: " ",
LuldReferencePriceTier: "2",
EtpFlag: "N",
MarketCategory: MKTCTG_NYSE_AMERICAN,
FinancialStatusIndicator: FSI_NOT_AVAILABLE,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseStockDirectory(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("ParseStockDirectory() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want) {
t.Errorf("%v", cmp.Diff(tt.want, got))
}
})
}
}

func TestStockDirectory_Bytes(t *testing.T) {
timestamp, _ := time.ParseDuration("3h7m14.939262551s")

tests := []struct {
name string
s StockDirectory
want []byte
}{
{
name: "BHB",
s: StockDirectory{
Timestamp: timestamp,
Stock: "BHB",
StockLocate: 849,
TrackingNumber: 0,
RoundLotSize: 100,
RoundLotsOnly: false,
IssueSubType: ICS_NOT_APPLICABLE,
IssueClassification: IC_COMMON_STOCK,
InverseIndicator: false,
Authenticity: AUTHENTICITY_LIVE,
EtpLeverageFactor: 0,
ShortSaleThresholdIndicator: "N",
IpoFlag: " ",
LuldReferencePriceTier: "2",
EtpFlag: "N",
MarketCategory: MKTCTG_NYSE_AMERICAN,
FinancialStatusIndicator: FSI_NOT_AVAILABLE,
},
want: []byte{82, 3, 81, 0, 0, 10, 55, 214, 144, 86, 87, 66, 72, 66, 32, 32, 32, 32, 32, 65, 32, 0, 0, 0, 100, 78, 67, 90, 32, 80, 78, 32, 50, 78, 0, 0, 0, 0, 78},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.Bytes(); !cmp.Equal(got, tt.want) {
t.Errorf("StockDirectory.Bytes() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 33d2378

Please sign in to comment.