github.com/iDigitalFlame/xmt@v0.5.4/com/flag_test.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package com
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/iDigitalFlame/xmt/data"
    23  )
    24  
    25  func TestFlag(t *testing.T) {
    26  	var f Flag
    27  	if n := f.Group(); n != 0 {
    28  		t.Fatalf(`TestFlag(): Group returned "%d" but should be "0"!`, n)
    29  	}
    30  	if f.SetGroup(0xBEEF); f.Group() != 0xBEEF {
    31  		t.Fatalf(`TestFlag(): Group returned "0x%X" but should be "0xBEEF"!`, f.Group())
    32  	}
    33  	if n := f.Position(); n != 0 {
    34  		t.Fatalf(`TestFlag(): Position returned "%d" but should be "0"!`, n)
    35  	}
    36  	if f.SetPosition(0xDEFF); f.Position() != 0xDEFF {
    37  		t.Fatalf(`TestFlag(): Position returned "0x%X" but should be "0xDEFF"!`, f.Position())
    38  	}
    39  	if n := f.Len(); n != 0 {
    40  		t.Fatalf(`TestFlag(): Len returned "%d" but should be "0"!`, n)
    41  	}
    42  	if f.SetLen(0xABCD); f.Len() != 0xABCD {
    43  		t.Fatalf(`TestFlag(): Len returned "0x%X" but should be "0xABCD"!`, f.Len())
    44  	}
    45  	if f&FlagFrag == 0 {
    46  		t.Fatalf("TestFlag(): Frag flag should be set!")
    47  	}
    48  	if f.Clear(); f != 0 {
    49  		t.Fatalf("TestFlag(): Flags should be empty!")
    50  	}
    51  	if f.Set(FlagCrypt); f != 256 {
    52  		t.Fatalf(`TestFlag(): Flag value is "%d" but should be "256"!`, f)
    53  	}
    54  	if f.Unset(FlagError); f != 256 {
    55  		t.Fatalf(`TestFlag(): Flag value is "%d" but should be "256"!`, f)
    56  	}
    57  	var c data.Chunk
    58  	if err := f.MarshalStream(&c); err != nil {
    59  		t.Fatalf("TestFlag(): MarshalStream returned an error: %s!", err.Error())
    60  	}
    61  	c.Seek(0, 0)
    62  	var g Flag
    63  	if err := g.UnmarshalStream(&c); err != nil {
    64  		t.Fatalf("TestFlag(): UnmarshalStream returned an error: %s!", err.Error())
    65  	}
    66  	if f != g {
    67  		t.Fatalf(`TestFlag(): Flag value is "%d" but should be "%d"!`, g, f)
    68  	}
    69  }