@@ -524,3 +524,67 @@ func TestFromBytesBE(t *testing.T) {
524524 t .Fatalf ("mismatch:\n %v !=\n %v" , u1 , u2 )
525525 }
526526}
527+
528+ func TestMarshal (t * testing.T ) {
529+ number := "42540766411282592856903984951653826562"
530+
531+ u , err := FromString (number )
532+ if err != nil {
533+ t .Fatal (err )
534+ }
535+
536+ btext , err := u .MarshalText ()
537+ if err != nil {
538+ t .Fatal (err )
539+ }
540+ bjson , err := u .MarshalJSON ()
541+ if err != nil {
542+ t .Fatal (err )
543+ }
544+
545+ expectedText := number
546+ if string (btext ) != expectedText {
547+ t .Fatalf ("text mismatch:\n %v !=\n %v" , string (btext ), expectedText )
548+ }
549+ expectedJSON := `"` + number + `"`
550+ if string (bjson ) != expectedJSON {
551+ t .Fatalf ("json mismatch:\n %v !=\n %v" , string (bjson ), expectedJSON )
552+ }
553+ }
554+
555+ func TestUnmarshal (t * testing.T ) {
556+ number := "42540766411282592856903984951653826562"
557+
558+ var u Uint128
559+ err := u .UnmarshalText ([]byte (number ))
560+ if err != nil {
561+ t .Fatal (err )
562+ }
563+
564+ expected , err := FromString (number )
565+ if err != nil {
566+ t .Fatal (err )
567+ }
568+ if u != expected {
569+ t .Fatalf ("text mismatch:\n %v !=\n %v" , u , expected )
570+ }
571+
572+ err = u .UnmarshalText ([]byte ("invalid" ))
573+ if err == nil {
574+ t .Fatal ("expected error" )
575+ }
576+
577+ err = u .UnmarshalJSON ([]byte (`"` + number + `"` ))
578+ if err != nil {
579+ t .Fatal (err )
580+ }
581+
582+ if u != expected {
583+ t .Fatalf ("json mismatch:\n %v !=\n %v" , u , expected )
584+ }
585+
586+ err = u .UnmarshalJSON ([]byte (`"invalid"` ))
587+ if err == nil {
588+ t .Fatal ("expected error" )
589+ }
590+ }
0 commit comments