...

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

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

     1  package exifcommon
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/dsoprea/go-logging"
    11  )
    12  
    13  func TestValueEncoder_encodeBytes__Cycle(t *testing.T) {
    14  	byteOrder := TestDefaultByteOrder
    15  	ve := NewValueEncoder(byteOrder)
    16  
    17  	original := []byte("original text")
    18  
    19  	ed, err := ve.encodeBytes(original)
    20  	log.PanicIf(err)
    21  
    22  	if ed.Type != TypeByte {
    23  		t.Fatalf("IFD type not expected.")
    24  	}
    25  
    26  	expected := []byte(original)
    27  
    28  	if reflect.DeepEqual(ed.Encoded, expected) != true {
    29  		t.Fatalf("Data not encoded correctly.")
    30  	} else if ed.UnitCount != 13 {
    31  		t.Fatalf("Unit-count not correct.")
    32  	}
    33  
    34  	recovered, err := parser.ParseBytes(ed.Encoded, ed.UnitCount)
    35  	log.PanicIf(err)
    36  
    37  	if reflect.DeepEqual(recovered, original) != true {
    38  		t.Fatalf("Value not recovered correctly.")
    39  	}
    40  }
    41  
    42  func TestValueEncoder_encodeAscii__Cycle(t *testing.T) {
    43  	byteOrder := TestDefaultByteOrder
    44  	ve := NewValueEncoder(byteOrder)
    45  
    46  	original := "original text"
    47  
    48  	ed, err := ve.encodeAscii(original)
    49  	log.PanicIf(err)
    50  
    51  	if ed.Type != TypeAscii {
    52  		t.Fatalf("IFD type not expected.")
    53  	}
    54  
    55  	expected := []byte(original)
    56  	expected = append(expected, 0)
    57  
    58  	if reflect.DeepEqual(ed.Encoded, expected) != true {
    59  		t.Fatalf("Data not encoded correctly.")
    60  	} else if ed.UnitCount != 14 {
    61  		t.Fatalf("Unit-count not correct.")
    62  	}
    63  
    64  	// Check that the string was recovered correctly and with the trailing NUL
    65  	// character autostripped.
    66  
    67  	recovered, err := parser.ParseAscii(ed.Encoded, ed.UnitCount)
    68  	log.PanicIf(err)
    69  
    70  	if reflect.DeepEqual(recovered, original) != true {
    71  		t.Fatalf("Value not recovered correctly.")
    72  	}
    73  }
    74  
    75  func TestValueEncoder_encodeAsciiNoNul__Cycle(t *testing.T) {
    76  	byteOrder := TestDefaultByteOrder
    77  	ve := NewValueEncoder(byteOrder)
    78  
    79  	original := "original text"
    80  
    81  	ed, err := ve.encodeAsciiNoNul(original)
    82  	log.PanicIf(err)
    83  
    84  	if ed.Type != TypeAsciiNoNul {
    85  		t.Fatalf("IFD type not expected.")
    86  	}
    87  
    88  	expected := []byte(original)
    89  
    90  	if reflect.DeepEqual(ed.Encoded, expected) != true {
    91  		t.Fatalf("Data not encoded correctly.")
    92  	} else if ed.UnitCount != 13 {
    93  		t.Fatalf("Unit-count not correct.")
    94  	}
    95  
    96  	// Check that the string was recovered correctly and with the trailing NUL
    97  	// character ignored (because not expected in the context of that type).
    98  
    99  	recovered, err := parser.ParseAsciiNoNul(ed.Encoded, ed.UnitCount)
   100  	log.PanicIf(err)
   101  
   102  	if reflect.DeepEqual(recovered, string(expected)) != true {
   103  		t.Fatalf("Value not recovered correctly.")
   104  	}
   105  }
   106  
   107  func TestValueEncoder_encodeShorts__Cycle(t *testing.T) {
   108  	byteOrder := TestDefaultByteOrder
   109  	ve := NewValueEncoder(byteOrder)
   110  
   111  	original := []uint16{0x11, 0x22, 0x33, 0x44, 0x55}
   112  
   113  	ed, err := ve.encodeShorts(original)
   114  	log.PanicIf(err)
   115  
   116  	if ed.Type != TypeShort {
   117  		t.Fatalf("IFD type not expected.")
   118  	}
   119  
   120  	expected := []byte{
   121  		0x00, 0x11,
   122  		0x00, 0x22,
   123  		0x00, 0x33,
   124  		0x00, 0x44,
   125  		0x00, 0x55,
   126  	}
   127  
   128  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   129  		t.Fatalf("Data not encoded correctly.")
   130  	} else if ed.UnitCount != 5 {
   131  		t.Fatalf("Unit-count not correct.")
   132  	}
   133  
   134  	recovered, err := parser.ParseShorts(ed.Encoded, ed.UnitCount, byteOrder)
   135  	log.PanicIf(err)
   136  
   137  	if reflect.DeepEqual(recovered, original) != true {
   138  		t.Fatalf("Value not recovered correctly.")
   139  	}
   140  }
   141  
   142  func TestValueEncoder_encodeLongs__Cycle(t *testing.T) {
   143  	byteOrder := TestDefaultByteOrder
   144  	ve := NewValueEncoder(byteOrder)
   145  
   146  	original := []uint32{0x11, 0x22, 0x33, 0x44, 0x55}
   147  
   148  	ed, err := ve.encodeLongs(original)
   149  	log.PanicIf(err)
   150  
   151  	if ed.Type != TypeLong {
   152  		t.Fatalf("IFD type not expected.")
   153  	}
   154  
   155  	expected := []byte{
   156  		0x00, 0x00, 0x00, 0x11,
   157  		0x00, 0x00, 0x00, 0x22,
   158  		0x00, 0x00, 0x00, 0x33,
   159  		0x00, 0x00, 0x00, 0x44,
   160  		0x00, 0x00, 0x00, 0x55,
   161  	}
   162  
   163  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   164  		t.Fatalf("Data not encoded correctly.")
   165  	} else if ed.UnitCount != 5 {
   166  		t.Fatalf("Unit-count not correct.")
   167  	}
   168  
   169  	recovered, err := parser.ParseLongs(ed.Encoded, ed.UnitCount, byteOrder)
   170  	log.PanicIf(err)
   171  
   172  	if reflect.DeepEqual(recovered, original) != true {
   173  		t.Fatalf("Value not recovered correctly.")
   174  	}
   175  }
   176  
   177  func TestValueEncoder_encodeFloats__Cycle(t *testing.T) {
   178  	byteOrder := TestDefaultByteOrder
   179  	ve := NewValueEncoder(byteOrder)
   180  
   181  	original := []float32{3.14159265, 2.71828182, 51.0, 68.0, 85.0}
   182  
   183  	ed, err := ve.encodeFloats(original)
   184  	log.PanicIf(err)
   185  
   186  	if ed.Type != TypeFloat {
   187  		t.Fatalf("IFD type not expected.")
   188  	}
   189  
   190  	expected := []byte{
   191  		0x40, 0x49, 0x0f, 0xdb,
   192  		0x40, 0x2d, 0xf8, 0x54,
   193  		0x42, 0x4c, 0x00, 0x00,
   194  		0x42, 0x88, 0x00, 0x00,
   195  		0x42, 0xaa, 0x00, 0x00,
   196  	}
   197  
   198  	if bytes.Equal(ed.Encoded, expected) != true {
   199  		t.Fatalf("Data not encoded correctly.")
   200  	} else if ed.UnitCount != 5 {
   201  		t.Fatalf("Unit-count not correct.")
   202  	}
   203  
   204  	recovered, err := parser.ParseFloats(ed.Encoded, ed.UnitCount, byteOrder)
   205  	log.PanicIf(err)
   206  
   207  	for i, v := range recovered {
   208  		if v < original[i] || v >= math.Nextafter32(original[i], original[i]+1) {
   209  			t.Fatalf("ReadFloats expecting %v, received %v", original[i], v)
   210  		}
   211  	}
   212  }
   213  
   214  func TestValueEncoder_encodeDoubles__Cycle(t *testing.T) {
   215  	byteOrder := TestDefaultByteOrder
   216  	ve := NewValueEncoder(byteOrder)
   217  
   218  	original := []float64{3.14159265, 2.71828182, 954877.1230695, 68.0, 85.0}
   219  
   220  	ed, err := ve.encodeDoubles(original)
   221  	log.PanicIf(err)
   222  
   223  	if ed.Type != TypeDouble {
   224  		t.Fatalf("IFD type not expected.")
   225  	}
   226  
   227  	expected := []byte{
   228  		0x40, 0x09, 0x21, 0xfb, 0x53, 0xc8, 0xd4, 0xf1,
   229  		0x40, 0x05, 0xbf, 0x0a, 0x89, 0xf1, 0xb0, 0xdd,
   230  		0x41, 0x2d, 0x23, 0xfa, 0x3f, 0x02, 0xf7, 0x2b,
   231  		0x40, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   232  		0x40, 0x55, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
   233  	}
   234  
   235  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   236  		t.Fatalf("Data not encoded correctly.")
   237  	} else if ed.UnitCount != 5 {
   238  		t.Fatalf("Unit-count not correct.")
   239  	}
   240  
   241  	recovered, err := parser.ParseDoubles(ed.Encoded, ed.UnitCount, byteOrder)
   242  	log.PanicIf(err)
   243  
   244  	for i, v := range recovered {
   245  		if v < original[i] || v >= math.Nextafter(original[i], original[i]+1) {
   246  			t.Fatalf("ReadDoubles expecting %v, received %v", original[i], v)
   247  		}
   248  	}
   249  }
   250  
   251  func TestValueEncoder_encodeRationals__Cycle(t *testing.T) {
   252  	byteOrder := TestDefaultByteOrder
   253  	ve := NewValueEncoder(byteOrder)
   254  
   255  	original := []Rational{
   256  		{
   257  			Numerator:   0x11,
   258  			Denominator: 0x22,
   259  		},
   260  		{
   261  			Numerator:   0x33,
   262  			Denominator: 0x44,
   263  		},
   264  		{
   265  			Numerator:   0x55,
   266  			Denominator: 0x66,
   267  		},
   268  		{
   269  			Numerator:   0x77,
   270  			Denominator: 0x88,
   271  		},
   272  		{
   273  			Numerator:   0x99,
   274  			Denominator: 0x00,
   275  		},
   276  	}
   277  
   278  	ed, err := ve.encodeRationals(original)
   279  	log.PanicIf(err)
   280  
   281  	if ed.Type != TypeRational {
   282  		t.Fatalf("IFD type not expected.")
   283  	}
   284  
   285  	expected := []byte{
   286  		0x00, 0x00, 0x00, 0x11,
   287  		0x00, 0x00, 0x00, 0x22,
   288  		0x00, 0x00, 0x00, 0x33,
   289  		0x00, 0x00, 0x00, 0x44,
   290  		0x00, 0x00, 0x00, 0x55,
   291  		0x00, 0x00, 0x00, 0x66,
   292  		0x00, 0x00, 0x00, 0x77,
   293  		0x00, 0x00, 0x00, 0x88,
   294  		0x00, 0x00, 0x00, 0x99,
   295  		0x00, 0x00, 0x00, 0x00,
   296  	}
   297  
   298  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   299  		t.Fatalf("Data not encoded correctly.")
   300  	} else if ed.UnitCount != 5 {
   301  		t.Fatalf("Unit-count not correct.")
   302  	}
   303  
   304  	recovered, err := parser.ParseRationals(ed.Encoded, ed.UnitCount, byteOrder)
   305  	log.PanicIf(err)
   306  
   307  	if reflect.DeepEqual(recovered, original) != true {
   308  		t.Fatalf("Value not recovered correctly.")
   309  	}
   310  }
   311  
   312  func TestValueEncoder_encodeSignedLongs__Cycle(t *testing.T) {
   313  	byteOrder := TestDefaultByteOrder
   314  	ve := NewValueEncoder(byteOrder)
   315  
   316  	original := []int32{0x11, 0x22, 0x33, 0x44, 0x55}
   317  
   318  	ed, err := ve.encodeSignedLongs(original)
   319  	log.PanicIf(err)
   320  
   321  	if ed.Type != TypeSignedLong {
   322  		t.Fatalf("IFD type not expected.")
   323  	}
   324  
   325  	expected := []byte{
   326  		0x00, 0x00, 0x00, 0x11,
   327  		0x00, 0x00, 0x00, 0x22,
   328  		0x00, 0x00, 0x00, 0x33,
   329  		0x00, 0x00, 0x00, 0x44,
   330  		0x00, 0x00, 0x00, 0x55,
   331  	}
   332  
   333  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   334  		t.Fatalf("Data not encoded correctly.")
   335  	} else if ed.UnitCount != 5 {
   336  		t.Fatalf("Unit-count not correct.")
   337  	}
   338  
   339  	recovered, err := parser.ParseSignedLongs(ed.Encoded, ed.UnitCount, byteOrder)
   340  	log.PanicIf(err)
   341  
   342  	if reflect.DeepEqual(recovered, original) != true {
   343  		t.Fatalf("Value not recovered correctly.")
   344  	}
   345  }
   346  
   347  func TestValueEncoder_encodeSignedRationals__Cycle(t *testing.T) {
   348  	byteOrder := TestDefaultByteOrder
   349  	ve := NewValueEncoder(byteOrder)
   350  
   351  	original := []SignedRational{
   352  		{
   353  			Numerator:   0x11,
   354  			Denominator: 0x22,
   355  		},
   356  		{
   357  			Numerator:   0x33,
   358  			Denominator: 0x44,
   359  		},
   360  		{
   361  			Numerator:   0x55,
   362  			Denominator: 0x66,
   363  		},
   364  		{
   365  			Numerator:   0x77,
   366  			Denominator: 0x88,
   367  		},
   368  		{
   369  			Numerator:   0x99,
   370  			Denominator: 0x00,
   371  		},
   372  	}
   373  
   374  	ed, err := ve.encodeSignedRationals(original)
   375  	log.PanicIf(err)
   376  
   377  	if ed.Type != TypeSignedRational {
   378  		t.Fatalf("IFD type not expected.")
   379  	}
   380  
   381  	expected := []byte{
   382  		0x00, 0x00, 0x00, 0x11,
   383  		0x00, 0x00, 0x00, 0x22,
   384  		0x00, 0x00, 0x00, 0x33,
   385  		0x00, 0x00, 0x00, 0x44,
   386  		0x00, 0x00, 0x00, 0x55,
   387  		0x00, 0x00, 0x00, 0x66,
   388  		0x00, 0x00, 0x00, 0x77,
   389  		0x00, 0x00, 0x00, 0x88,
   390  		0x00, 0x00, 0x00, 0x99,
   391  		0x00, 0x00, 0x00, 0x00,
   392  	}
   393  
   394  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   395  		t.Fatalf("Data not encoded correctly.")
   396  	} else if ed.UnitCount != 5 {
   397  		t.Fatalf("Unit-count not correct.")
   398  	}
   399  
   400  	recovered, err := parser.ParseSignedRationals(ed.Encoded, ed.UnitCount, byteOrder)
   401  	log.PanicIf(err)
   402  
   403  	if reflect.DeepEqual(recovered, original) != true {
   404  		t.Fatalf("Value not recovered correctly.")
   405  	}
   406  }
   407  
   408  func TestValueEncoder_Encode__Byte(t *testing.T) {
   409  	byteOrder := TestDefaultByteOrder
   410  	ve := NewValueEncoder(byteOrder)
   411  
   412  	original := []byte("original text")
   413  
   414  	ed, err := ve.Encode(original)
   415  	log.PanicIf(err)
   416  
   417  	if ed.Type != TypeByte {
   418  		t.Fatalf("IFD type not expected.")
   419  	}
   420  
   421  	expected := []byte(original)
   422  
   423  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   424  		t.Fatalf("Data not encoded correctly.")
   425  	} else if ed.UnitCount != 13 {
   426  		t.Fatalf("Unit-count not correct.")
   427  	}
   428  }
   429  
   430  func TestValueEncoder_Encode__Ascii(t *testing.T) {
   431  	byteOrder := TestDefaultByteOrder
   432  	ve := NewValueEncoder(byteOrder)
   433  
   434  	original := "original text"
   435  
   436  	ed, err := ve.Encode(original)
   437  	log.PanicIf(err)
   438  
   439  	if ed.Type != TypeAscii {
   440  		t.Fatalf("IFD type not expected.")
   441  	}
   442  
   443  	expected := []byte(original)
   444  	expected = append(expected, 0)
   445  
   446  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   447  		t.Fatalf("Data not encoded correctly.")
   448  	} else if ed.UnitCount != 14 {
   449  		t.Fatalf("Unit-count not correct.")
   450  	}
   451  }
   452  
   453  func TestValueEncoder_Encode__Short(t *testing.T) {
   454  	byteOrder := TestDefaultByteOrder
   455  	ve := NewValueEncoder(byteOrder)
   456  
   457  	original := []uint16{0x11, 0x22, 0x33, 0x44, 0x55}
   458  
   459  	ed, err := ve.Encode(original)
   460  	log.PanicIf(err)
   461  
   462  	if ed.Type != TypeShort {
   463  		t.Fatalf("IFD type not expected.")
   464  	}
   465  
   466  	expected := []byte{
   467  		0x00, 0x11,
   468  		0x00, 0x22,
   469  		0x00, 0x33,
   470  		0x00, 0x44,
   471  		0x00, 0x55,
   472  	}
   473  
   474  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   475  		t.Fatalf("Data not encoded correctly.")
   476  	} else if ed.UnitCount != 5 {
   477  		t.Fatalf("Unit-count not correct.")
   478  	}
   479  }
   480  
   481  func TestValueEncoder_Encode__Long(t *testing.T) {
   482  	byteOrder := TestDefaultByteOrder
   483  	ve := NewValueEncoder(byteOrder)
   484  
   485  	original := []uint32{0x11, 0x22, 0x33, 0x44, 0x55}
   486  
   487  	ed, err := ve.Encode(original)
   488  	log.PanicIf(err)
   489  
   490  	if ed.Type != TypeLong {
   491  		t.Fatalf("IFD type not expected.")
   492  	}
   493  
   494  	expected := []byte{
   495  		0x00, 0x00, 0x00, 0x11,
   496  		0x00, 0x00, 0x00, 0x22,
   497  		0x00, 0x00, 0x00, 0x33,
   498  		0x00, 0x00, 0x00, 0x44,
   499  		0x00, 0x00, 0x00, 0x55,
   500  	}
   501  
   502  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   503  		t.Fatalf("Data not encoded correctly.")
   504  	} else if ed.UnitCount != 5 {
   505  		t.Fatalf("Unit-count not correct.")
   506  	}
   507  }
   508  
   509  func TestValueEncoder_Encode__Float(t *testing.T) {
   510  	byteOrder := TestDefaultByteOrder
   511  	ve := NewValueEncoder(byteOrder)
   512  
   513  	original := []float32{3.14159265, 2.71828182, 51.0, 68.0, 85.0}
   514  
   515  	ed, err := ve.Encode(original)
   516  	log.PanicIf(err)
   517  
   518  	if ed.Type != TypeFloat {
   519  		t.Fatalf("IFD type not expected.")
   520  	}
   521  
   522  	expected := []byte{
   523  		0x40, 0x49, 0x0f, 0xdb,
   524  		0x40, 0x2d, 0xf8, 0x54,
   525  		0x42, 0x4c, 0x00, 0x00,
   526  		0x42, 0x88, 0x00, 0x00,
   527  		0x42, 0xaa, 0x00, 0x00,
   528  	}
   529  
   530  	if bytes.Equal(ed.Encoded, expected) != true {
   531  		t.Fatalf("Data not encoded correctly.")
   532  	} else if ed.UnitCount != 5 {
   533  		t.Fatalf("Unit-count not correct.")
   534  	}
   535  
   536  }
   537  
   538  func TestValueEncoder_Encode__Double(t *testing.T) {
   539  	byteOrder := TestDefaultByteOrder
   540  	ve := NewValueEncoder(byteOrder)
   541  
   542  	original := []float64{3.14159265, 2.71828182, 954877.1230695, 68.0, 85.0}
   543  
   544  	ed, err := ve.Encode(original)
   545  	log.PanicIf(err)
   546  
   547  	if ed.Type != TypeDouble {
   548  		t.Fatalf("IFD type not expected.")
   549  	}
   550  
   551  	expected := []byte{
   552  		0x40, 0x09, 0x21, 0xfb, 0x53, 0xc8, 0xd4, 0xf1,
   553  		0x40, 0x05, 0xbf, 0x0a, 0x89, 0xf1, 0xb0, 0xdd,
   554  		0x41, 0x2d, 0x23, 0xfa, 0x3f, 0x02, 0xf7, 0x2b,
   555  		0x40, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   556  		0x40, 0x55, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
   557  	}
   558  
   559  	if bytes.Equal(ed.Encoded, expected) != true {
   560  		t.Fatalf("Data not encoded correctly.")
   561  	} else if ed.UnitCount != 5 {
   562  		t.Fatalf("Unit-count not correct.")
   563  	}
   564  
   565  }
   566  
   567  func TestValueEncoder_Encode__Rational(t *testing.T) {
   568  	byteOrder := TestDefaultByteOrder
   569  	ve := NewValueEncoder(byteOrder)
   570  
   571  	original := []Rational{
   572  		{
   573  			Numerator:   0x11,
   574  			Denominator: 0x22,
   575  		},
   576  		{
   577  			Numerator:   0x33,
   578  			Denominator: 0x44,
   579  		},
   580  		{
   581  			Numerator:   0x55,
   582  			Denominator: 0x66,
   583  		},
   584  		{
   585  			Numerator:   0x77,
   586  			Denominator: 0x88,
   587  		},
   588  		{
   589  			Numerator:   0x99,
   590  			Denominator: 0x00,
   591  		},
   592  	}
   593  
   594  	ed, err := ve.Encode(original)
   595  	log.PanicIf(err)
   596  
   597  	if ed.Type != TypeRational {
   598  		t.Fatalf("IFD type not expected.")
   599  	}
   600  
   601  	expected := []byte{
   602  		0x00, 0x00, 0x00, 0x11,
   603  		0x00, 0x00, 0x00, 0x22,
   604  		0x00, 0x00, 0x00, 0x33,
   605  		0x00, 0x00, 0x00, 0x44,
   606  		0x00, 0x00, 0x00, 0x55,
   607  		0x00, 0x00, 0x00, 0x66,
   608  		0x00, 0x00, 0x00, 0x77,
   609  		0x00, 0x00, 0x00, 0x88,
   610  		0x00, 0x00, 0x00, 0x99,
   611  		0x00, 0x00, 0x00, 0x00,
   612  	}
   613  
   614  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   615  		t.Fatalf("Data not encoded correctly.")
   616  	} else if ed.UnitCount != 5 {
   617  		t.Fatalf("Unit-count not correct.")
   618  	}
   619  }
   620  
   621  func TestValueEncoder_Encode__SignedLong(t *testing.T) {
   622  	byteOrder := TestDefaultByteOrder
   623  	ve := NewValueEncoder(byteOrder)
   624  
   625  	original := []int32{0x11, 0x22, 0x33, 0x44, 0x55}
   626  
   627  	ed, err := ve.Encode(original)
   628  	log.PanicIf(err)
   629  
   630  	if ed.Type != TypeSignedLong {
   631  		t.Fatalf("IFD type not expected.")
   632  	}
   633  
   634  	expected := []byte{
   635  		0x00, 0x00, 0x00, 0x11,
   636  		0x00, 0x00, 0x00, 0x22,
   637  		0x00, 0x00, 0x00, 0x33,
   638  		0x00, 0x00, 0x00, 0x44,
   639  		0x00, 0x00, 0x00, 0x55,
   640  	}
   641  
   642  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   643  		t.Fatalf("Data not encoded correctly.")
   644  	} else if ed.UnitCount != 5 {
   645  		t.Fatalf("Unit-count not correct.")
   646  	}
   647  }
   648  
   649  func TestValueEncoder_Encode__SignedRational(t *testing.T) {
   650  	byteOrder := TestDefaultByteOrder
   651  	ve := NewValueEncoder(byteOrder)
   652  
   653  	original := []SignedRational{
   654  		{
   655  			Numerator:   0x11,
   656  			Denominator: 0x22,
   657  		},
   658  		{
   659  			Numerator:   0x33,
   660  			Denominator: 0x44,
   661  		},
   662  		{
   663  			Numerator:   0x55,
   664  			Denominator: 0x66,
   665  		},
   666  		{
   667  			Numerator:   0x77,
   668  			Denominator: 0x88,
   669  		},
   670  		{
   671  			Numerator:   0x99,
   672  			Denominator: 0x00,
   673  		},
   674  	}
   675  
   676  	ed, err := ve.Encode(original)
   677  	log.PanicIf(err)
   678  
   679  	if ed.Type != TypeSignedRational {
   680  		t.Fatalf("IFD type not expected.")
   681  	}
   682  
   683  	expected := []byte{
   684  		0x00, 0x00, 0x00, 0x11,
   685  		0x00, 0x00, 0x00, 0x22,
   686  		0x00, 0x00, 0x00, 0x33,
   687  		0x00, 0x00, 0x00, 0x44,
   688  		0x00, 0x00, 0x00, 0x55,
   689  		0x00, 0x00, 0x00, 0x66,
   690  		0x00, 0x00, 0x00, 0x77,
   691  		0x00, 0x00, 0x00, 0x88,
   692  		0x00, 0x00, 0x00, 0x99,
   693  		0x00, 0x00, 0x00, 0x00,
   694  	}
   695  
   696  	if reflect.DeepEqual(ed.Encoded, expected) != true {
   697  		t.Fatalf("Data not encoded correctly.")
   698  	} else if ed.UnitCount != 5 {
   699  		t.Fatalf("Unit-count not correct.")
   700  	}
   701  }
   702  
   703  func TestValueEncoder_Encode__Timestamp(t *testing.T) {
   704  	byteOrder := TestDefaultByteOrder
   705  	ve := NewValueEncoder(byteOrder)
   706  
   707  	now := time.Now()
   708  
   709  	ed, err := ve.Encode(now)
   710  	log.PanicIf(err)
   711  
   712  	if ed.Type != TypeAscii {
   713  		t.Fatalf("Timestamp not encoded as ASCII.")
   714  	}
   715  
   716  	expectedTimestampBytes := ExifFullTimestampString(now)
   717  
   718  	// Leave an extra byte for the NUL.
   719  	expected := make([]byte, len(expectedTimestampBytes)+1)
   720  	copy(expected, expectedTimestampBytes)
   721  
   722  	if bytes.Equal(ed.Encoded, expected) != true {
   723  		t.Fatalf("Timestamp not encoded correctly: [%s] != [%s]", string(ed.Encoded), string(expected))
   724  	}
   725  }
   726  

View as plain text