...

Source file src/github.com/dsoprea/go-exif/v3/common/type_test.go

Documentation: github.com/dsoprea/go-exif/v3/common

     1  package exifcommon
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/dsoprea/go-logging"
     8  )
     9  
    10  func TestTypeByte_String(t *testing.T) {
    11  	if TypeByte.String() != "BYTE" {
    12  		t.Fatalf("Type name not correct (byte): [%s]", TypeByte.String())
    13  	}
    14  }
    15  
    16  func TestTypeAscii_String(t *testing.T) {
    17  	if TypeAscii.String() != "ASCII" {
    18  		t.Fatalf("Type name not correct (ASCII): [%s]", TypeAscii.String())
    19  	}
    20  }
    21  
    22  func TestTypeAsciiNoNul_String(t *testing.T) {
    23  	if TypeAsciiNoNul.String() != "_ASCII_NO_NUL" {
    24  		t.Fatalf("Type name not correct (ASCII no-NUL): [%s]", TypeAsciiNoNul.String())
    25  	}
    26  }
    27  
    28  func TestTypeShort_String(t *testing.T) {
    29  	if TypeShort.String() != "SHORT" {
    30  		t.Fatalf("Type name not correct (short): [%s]", TypeShort.String())
    31  	}
    32  }
    33  
    34  func TestTypeLong_String(t *testing.T) {
    35  	if TypeLong.String() != "LONG" {
    36  		t.Fatalf("Type name not correct (long): [%s]", TypeLong.String())
    37  	}
    38  }
    39  
    40  func TestTypeRational_String(t *testing.T) {
    41  	if TypeRational.String() != "RATIONAL" {
    42  		t.Fatalf("Type name not correct (rational): [%s]", TypeRational.String())
    43  	}
    44  }
    45  
    46  func TestTypeSignedLong_String(t *testing.T) {
    47  	if TypeSignedLong.String() != "SLONG" {
    48  		t.Fatalf("Type name not correct (signed long): [%s]", TypeSignedLong.String())
    49  	}
    50  }
    51  
    52  func TestTypeSignedRational_String(t *testing.T) {
    53  	if TypeSignedRational.String() != "SRATIONAL" {
    54  		t.Fatalf("Type name not correct (signed rational): [%s]", TypeSignedRational.String())
    55  	}
    56  }
    57  
    58  func TestTypeFloat_String(t *testing.T) {
    59  	if TypeFloat.String() != "FLOAT" {
    60  		t.Fatalf("Type name not correct (float): [%s]", TypeFloat.String())
    61  	}
    62  }
    63  
    64  func TestTypeDouble_String(t *testing.T) {
    65  	if TypeDouble.String() != "DOUBLE" {
    66  		t.Fatalf("Type name not correct (double): [%s]", TypeDouble.String())
    67  	}
    68  }
    69  
    70  func TestTypeByte_Size(t *testing.T) {
    71  	if TypeByte.Size() != 1 {
    72  		t.Fatalf("Type size not correct (byte): (%d)", TypeByte.Size())
    73  	}
    74  }
    75  
    76  func TestTypeAscii_Size(t *testing.T) {
    77  	if TypeAscii.Size() != 1 {
    78  		t.Fatalf("Type size not correct (ASCII): (%d)", TypeAscii.Size())
    79  	}
    80  }
    81  
    82  func TestTypeAsciiNoNul_Size(t *testing.T) {
    83  	if TypeAsciiNoNul.Size() != 1 {
    84  		t.Fatalf("Type size not correct (ASCII no-NUL): (%d)", TypeAsciiNoNul.Size())
    85  	}
    86  }
    87  
    88  func TestTypeShort_Size(t *testing.T) {
    89  	if TypeShort.Size() != 2 {
    90  		t.Fatalf("Type size not correct (short): (%d)", TypeShort.Size())
    91  	}
    92  }
    93  
    94  func TestTypeLong_Size(t *testing.T) {
    95  	if TypeLong.Size() != 4 {
    96  		t.Fatalf("Type size not correct (long): (%d)", TypeLong.Size())
    97  	}
    98  }
    99  
   100  func TestTypeRational_Size(t *testing.T) {
   101  	if TypeRational.Size() != 8 {
   102  		t.Fatalf("Type size not correct (rational): (%d)", TypeRational.Size())
   103  	}
   104  }
   105  
   106  func TestTypeSignedLong_Size(t *testing.T) {
   107  	if TypeSignedLong.Size() != 4 {
   108  		t.Fatalf("Type size not correct (signed long): (%d)", TypeSignedLong.Size())
   109  	}
   110  }
   111  
   112  func TestTypeSignedRational_Size(t *testing.T) {
   113  	if TypeSignedRational.Size() != 8 {
   114  		t.Fatalf("Type size not correct (signed rational): (%d)", TypeSignedRational.Size())
   115  	}
   116  }
   117  
   118  func TestTypeFloat_Size(t *testing.T) {
   119  	if TypeFloat.Size() != 4 {
   120  		t.Fatalf("Type size not correct (float): (%d)", TypeFloat.Size())
   121  	}
   122  }
   123  
   124  func TestTypeDouble_Size(t *testing.T) {
   125  	if TypeDouble.Size() != 8 {
   126  		t.Fatalf("Type size not correct (double): (%d)", TypeDouble.Size())
   127  	}
   128  }
   129  
   130  func TestFormat__Byte(t *testing.T) {
   131  	r := []byte{1, 2, 3, 4, 5, 6, 7, 8}
   132  
   133  	s, err := FormatFromBytes(r, TypeByte, false, TestDefaultByteOrder)
   134  	log.PanicIf(err)
   135  
   136  	if s != "01 02 03 04 05 06 07 08" {
   137  		t.Fatalf("Format output not correct (bytes): [%s]", s)
   138  	}
   139  }
   140  
   141  func TestFormat__Ascii(t *testing.T) {
   142  	r := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 0}
   143  
   144  	s, err := FormatFromBytes(r, TypeAscii, false, TestDefaultByteOrder)
   145  	log.PanicIf(err)
   146  
   147  	if s != "abcdefg" {
   148  		t.Fatalf("Format output not correct (ASCII): [%s]", s)
   149  	}
   150  }
   151  
   152  func TestFormat__AsciiNoNul(t *testing.T) {
   153  	r := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
   154  
   155  	s, err := FormatFromBytes(r, TypeAsciiNoNul, false, TestDefaultByteOrder)
   156  	log.PanicIf(err)
   157  
   158  	if s != "abcdefgh" {
   159  		t.Fatalf("Format output not correct (ASCII no-NUL): [%s]", s)
   160  	}
   161  }
   162  
   163  func TestFormat__Short(t *testing.T) {
   164  	r := []byte{0, 1, 0, 2}
   165  
   166  	s, err := FormatFromBytes(r, TypeShort, false, TestDefaultByteOrder)
   167  	log.PanicIf(err)
   168  
   169  	if s != "[1 2]" {
   170  		t.Fatalf("Format output not correct (shorts): [%s]", s)
   171  	}
   172  }
   173  
   174  func TestFormat__Long(t *testing.T) {
   175  	r := []byte{0, 0, 0, 1, 0, 0, 0, 2}
   176  
   177  	s, err := FormatFromBytes(r, TypeLong, false, TestDefaultByteOrder)
   178  	log.PanicIf(err)
   179  
   180  	if s != "[1 2]" {
   181  		t.Fatalf("Format output not correct (longs): [%s]", s)
   182  	}
   183  }
   184  
   185  func TestFormat__Float(t *testing.T) {
   186  	r := []byte{0x3f, 0x80, 0x00, 0x00,
   187  		0x40, 0x00, 0x00, 0x00}
   188  
   189  	s, err := FormatFromBytes(r, TypeFloat, false, TestDefaultByteOrder)
   190  	log.PanicIf(err)
   191  
   192  	if s != "[1 2]" {
   193  		t.Fatalf("Format output not correct (floats): [%s]", s)
   194  	}
   195  }
   196  
   197  func TestFormat__Double(t *testing.T) {
   198  	r := []byte{0x40, 0x09, 0x21, 0xfb, 0x53, 0xc8, 0xd4, 0xf1,
   199  		0x40, 0x05, 0xbf, 0x0a, 0x89, 0xf1, 0xb0, 0xdd}
   200  
   201  	s, err := FormatFromBytes(r, TypeDouble, false, TestDefaultByteOrder)
   202  	log.PanicIf(err)
   203  
   204  	if s != "[3.14159265 2.71828182]" {
   205  		t.Fatalf("Format output not correct (doubles): [%s]", s)
   206  	}
   207  }
   208  
   209  func TestFormat__Rational(t *testing.T) {
   210  	r := []byte{
   211  		0, 0, 0, 1, 0, 0, 0, 2,
   212  		0, 0, 0, 3, 0, 0, 0, 4,
   213  	}
   214  
   215  	s, err := FormatFromBytes(r, TypeRational, false, TestDefaultByteOrder)
   216  	log.PanicIf(err)
   217  
   218  	if s != "[1/2 3/4]" {
   219  		t.Fatalf("Format output not correct (rationals): [%s]", s)
   220  	}
   221  }
   222  
   223  func TestFormat__SignedLong(t *testing.T) {
   224  	r := []byte{0, 0, 0, 1, 0, 0, 0, 2}
   225  
   226  	s, err := FormatFromBytes(r, TypeSignedLong, false, TestDefaultByteOrder)
   227  	log.PanicIf(err)
   228  
   229  	if s != "[1 2]" {
   230  		t.Fatalf("Format output not correct (signed longs): [%s]", s)
   231  	}
   232  }
   233  
   234  func TestFormat__SignedRational(t *testing.T) {
   235  	r := []byte{
   236  		0, 0, 0, 1, 0, 0, 0, 2,
   237  		0, 0, 0, 3, 0, 0, 0, 4,
   238  	}
   239  
   240  	s, err := FormatFromBytes(r, TypeSignedRational, false, TestDefaultByteOrder)
   241  	log.PanicIf(err)
   242  
   243  	if s != "[1/2 3/4]" {
   244  		t.Fatalf("Format output not correct (signed rationals): [%s]", s)
   245  	}
   246  }
   247  
   248  func TestFormat__Undefined(t *testing.T) {
   249  	r := []byte{'a', 'b'}
   250  
   251  	_, err := FormatFromBytes(r, TypeUndefined, false, TestDefaultByteOrder)
   252  	if err == nil {
   253  		t.Fatalf("Expected error.")
   254  	} else if err.Error() != "can not determine tag-value size for type (7): [UNDEFINED]" {
   255  		log.Panic(err)
   256  	}
   257  }
   258  
   259  func TestTranslateStringToType__TypeUndefined(t *testing.T) {
   260  	_, err := TranslateStringToType(TypeUndefined, "")
   261  	if err == nil {
   262  		t.Fatalf("Expected error.")
   263  	} else if err.Error() != "undefined-type values are not supported" {
   264  		log.Panic(err)
   265  	}
   266  }
   267  
   268  func TestTranslateStringToType__TypeByte(t *testing.T) {
   269  	v, err := TranslateStringToType(TypeByte, "02")
   270  	log.PanicIf(err)
   271  
   272  	if v != byte(2) {
   273  		t.Fatalf("Translation of string to type not correct (bytes): %v", v)
   274  	}
   275  }
   276  
   277  func TestTranslateStringToType__TypeAscii(t *testing.T) {
   278  	v, err := TranslateStringToType(TypeAscii, "abcdefgh")
   279  	log.PanicIf(err)
   280  
   281  	if v != "abcdefgh" {
   282  		t.Fatalf("Translation of string to type not correct (ascii): %v", v)
   283  	}
   284  }
   285  
   286  func TestTranslateStringToType__TypeAsciiNoNul(t *testing.T) {
   287  	v, err := TranslateStringToType(TypeAsciiNoNul, "abcdefgh")
   288  	log.PanicIf(err)
   289  
   290  	if v != "abcdefgh" {
   291  		t.Fatalf("Translation of string to type not correct (ascii no-NUL): %v", v)
   292  	}
   293  }
   294  
   295  func TestTranslateStringToType__TypeShort(t *testing.T) {
   296  	v, err := TranslateStringToType(TypeShort, "11")
   297  	log.PanicIf(err)
   298  
   299  	if v != uint16(11) {
   300  		t.Fatalf("Translation of string to type not correct (short): %v", v)
   301  	}
   302  }
   303  
   304  func TestTranslateStringToType__TypeLong(t *testing.T) {
   305  	v, err := TranslateStringToType(TypeLong, "11")
   306  	log.PanicIf(err)
   307  
   308  	if v != uint32(11) {
   309  		t.Fatalf("Translation of string to type not correct (long): %v", v)
   310  	}
   311  }
   312  
   313  func TestTranslateStringToType__TypeFloat(t *testing.T) {
   314  	v, err := TranslateStringToType(TypeFloat, "3.14159265")
   315  	log.PanicIf(err)
   316  
   317  	expected := float32(3.14159265)
   318  	if v.(float32) < expected || v.(float32) >= math.Nextafter32(expected, expected+1) {
   319  		t.Fatalf("Translation of string to type not correct (float32): %v", v)
   320  	}
   321  }
   322  
   323  func TestTranslateStringToType__TypeDouble(t *testing.T) {
   324  	v, err := TranslateStringToType(TypeDouble, "3.14159265")
   325  	log.PanicIf(err)
   326  
   327  	expected := float64(3.14159265)
   328  	if v.(float64) < expected || v.(float64) >= math.Nextafter(expected, expected+1) {
   329  		t.Fatalf("Translation of string to type not correct (double): %v", v)
   330  	}
   331  }
   332  
   333  func TestTranslateStringToType__TypeRational(t *testing.T) {
   334  	v, err := TranslateStringToType(TypeRational, "11/22")
   335  	log.PanicIf(err)
   336  
   337  	r := v.(Rational)
   338  
   339  	if r.Numerator != 11 || r.Denominator != 22 {
   340  		t.Fatalf("Translation of string to type not correct (rational): %v", r)
   341  	}
   342  }
   343  
   344  func TestTranslateStringToType__TypeSignedLong(t *testing.T) {
   345  	v, err := TranslateStringToType(TypeSignedLong, "11")
   346  	log.PanicIf(err)
   347  
   348  	if v != int32(11) {
   349  		t.Fatalf("Translation of string to type not correct (signed long): %v", v)
   350  	}
   351  }
   352  
   353  func TestTranslateStringToType__TypeSignedRational(t *testing.T) {
   354  	v, err := TranslateStringToType(TypeSignedRational, "11/22")
   355  	log.PanicIf(err)
   356  
   357  	r := v.(SignedRational)
   358  
   359  	if r.Numerator != 11 || r.Denominator != 22 {
   360  		t.Fatalf("Translation of string to type not correct (signed rational): %v", r)
   361  	}
   362  }
   363  
   364  func TestTranslateStringToType__InvalidType(t *testing.T) {
   365  	_, err := TranslateStringToType(99, "11/22")
   366  	if err == nil {
   367  		t.Fatalf("Expected error for invalid type.")
   368  	} else if err.Error() != "from-string encoding for type not supported; this shouldn't happen: []" {
   369  		log.Panic(err)
   370  	}
   371  }
   372  
   373  //     } else if tagType == TypeLong {
   374  //         n, err := strconv.ParseUint(valueString, 10, 32)
   375  //         log.PanicIf(err)
   376  
   377  //         return uint32(n), nil
   378  //     } else if tagType == TypeRational {
   379  //         parts := strings.SplitN(valueString, "/", 2)
   380  
   381  //         numerator, err := strconv.ParseUint(parts[0], 10, 32)
   382  //         log.PanicIf(err)
   383  
   384  //         denominator, err := strconv.ParseUint(parts[1], 10, 32)
   385  //         log.PanicIf(err)
   386  
   387  //         return Rational{
   388  //             Numerator:   uint32(numerator),
   389  //             Denominator: uint32(denominator),
   390  //         }, nil
   391  //     } else if tagType == TypeSignedLong {
   392  //         n, err := strconv.ParseInt(valueString, 10, 32)
   393  //         log.PanicIf(err)
   394  
   395  //         return int32(n), nil
   396  //     } else if tagType == TypeSignedRational {
   397  //         parts := strings.SplitN(valueString, "/", 2)
   398  
   399  //         numerator, err := strconv.ParseInt(parts[0], 10, 32)
   400  //         log.PanicIf(err)
   401  
   402  //         denominator, err := strconv.ParseInt(parts[1], 10, 32)
   403  //         log.PanicIf(err)
   404  
   405  //         return SignedRational{
   406  //             Numerator:   int32(numerator),
   407  //             Denominator: int32(denominator),
   408  //         }, nil
   409  //     }
   410  
   411  //     log.Panicf("from-string encoding for type not supported; this shouldn't happen: [%s]", tagType.String())
   412  //     return nil, nil
   413  // }
   414  
   415  func TestIsPrintableText_letters(t *testing.T) {
   416  	if isPrintableText("abc") != true {
   417  		t.Fatalf("Printable text interpreted as nonprintable.")
   418  	}
   419  }
   420  
   421  func TestIsPrintableText_space(t *testing.T) {
   422  	if isPrintableText(" ") != true {
   423  		t.Fatalf("Printable text interpreted as nonprintable.")
   424  	}
   425  }
   426  
   427  func TestIsPrintableText_newlines(t *testing.T) {
   428  	if isPrintableText("\r\n") != true {
   429  		t.Fatalf("Printable text interpreted as nonprintable.")
   430  	}
   431  }
   432  
   433  func TestIsPrintableText_punctuationAndSymbols(t *testing.T) {
   434  	if isPrintableText(",:-/$©") != true {
   435  		t.Fatalf("Printable text interpreted as nonprintable.")
   436  	}
   437  }
   438  

View as plain text