Skip to content

Commit

Permalink
Avoid allocating empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
decriptor committed Jul 30, 2022
1 parent c853d26 commit 55c816c
Show file tree
Hide file tree
Showing 50 changed files with 161 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/TaglibSharp.Tests/Images/AddImageMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void AddXMPTest2 (string sample_file, string tmp_file, bool contai
xmp_tag.Comment = null;
xmp_tag.Software = test_comment;

Assert.AreEqual (new string[] { }, xmp_tag.Keywords);
Assert.AreEqual (Array.Empty<string> (), xmp_tag.Keywords);
Assert.AreEqual (null, xmp_tag.Comment);
Assert.AreEqual (test_comment, xmp_tag.Software);

Expand All @@ -119,7 +119,7 @@ public static void AddXMPTest2 (string sample_file, string tmp_file, bool contai
xmp_tag = file.GetTag (TagTypes.XMP, false) as XmpTag;
Assert.IsNotNull (xmp_tag, "XMP Tag not read");

Assert.AreEqual (new string[] { }, xmp_tag.Keywords);
Assert.AreEqual (Array.Empty<string> (), xmp_tag.Keywords);
Assert.AreEqual (null, xmp_tag.Comment);
Assert.AreEqual (test_comment, xmp_tag.Software);
}
Expand Down
8 changes: 4 additions & 4 deletions src/TaglibSharp.Tests/Images/CopyFromTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public void TestJPGtoTIFF ()
Assert.AreEqual (TagTypes.TiffIFD | TagTypes.XMP, file2.TagTypes);
Assert.AreEqual ("%test comment%", file1.ImageTag.Comment);
Assert.AreEqual (string.Empty, file2.ImageTag.Comment);
Assert.AreEqual (new string[] { }, file1.ImageTag.Keywords);
Assert.AreEqual (new string[] { }, file2.ImageTag.Keywords);
Assert.AreEqual (Array.Empty<string> (), file1.ImageTag.Keywords);
Assert.AreEqual (Array.Empty<string> (), file2.ImageTag.Keywords);
Assert.AreEqual (null, file1.ImageTag.Rating);
Assert.AreEqual (0, file2.ImageTag.Rating);
Assert.AreEqual (new DateTime (2009, 8, 9, 19, 12, 44), (DateTime)file1.ImageTag.DateTime);
Expand Down Expand Up @@ -62,8 +62,8 @@ public void TestJPGtoTIFF ()
Assert.AreEqual (TagTypes.TiffIFD | TagTypes.XMP, file2.TagTypes);
Assert.AreEqual ("%test comment%", file1.ImageTag.Comment);
Assert.AreEqual ("%test comment%", file2.ImageTag.Comment);
Assert.AreEqual (new string[] { }, file1.ImageTag.Keywords);
Assert.AreEqual (new string[] { }, file2.ImageTag.Keywords);
Assert.AreEqual (Array.Empty<string> (), file1.ImageTag.Keywords);
Assert.AreEqual (Array.Empty<string> (), file2.ImageTag.Keywords);
Assert.AreEqual (null, file1.ImageTag.Rating);
Assert.AreEqual (null, file2.ImageTag.Rating);
Assert.AreEqual (new DateTime (2009, 8, 9, 19, 12, 44), (DateTime)file1.ImageTag.DateTime);
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/GifExiftoolLongCommentTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TaglibSharp.Tests.Images.Validators;
Expand All @@ -20,7 +22,7 @@ public void Test ()
true,
new GifExiftoolLongCommentTestInvariantValidator (),
NoModificationValidator.Instance,
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true),
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true),
new CommentModificationValidator (long_comment_orig),
new CommentModificationValidator (long_comment_orig, long_comment_test),
new TagCommentModificationValidator (long_comment_orig, TagTypes.GifComment, true),
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/GifExiftoolTangled3Test.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TaglibSharp.Tests.Images.Validators;
Expand All @@ -19,7 +21,7 @@ public void Test ()
true,
new GifExiftoolTangled3TestInvariantValidator (),
NoModificationValidator.Instance,
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, false),
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, false),
new CommentModificationValidator ("Created with GIMP"),
new TagCommentModificationValidator ("Created with GIMP", TagTypes.GifComment, true),
new RemoveMetadataValidator (TagTypes.GifComment)
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/ImageTagTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.Image;
Expand All @@ -20,7 +22,7 @@ public void TestXMPImageTag ()
Assert.IsNotNull (tag);

Assert.AreEqual (null, tag.Comment, "Comment");
Assert.AreEqual (new string[] { }, tag.Keywords, "Keywords");
Assert.AreEqual (Array.Empty<string> (), tag.Keywords, "Keywords");
Assert.AreEqual (0, tag.Rating, "Rating");
Assert.AreEqual (null, tag.DateTime, "DateTime");
Assert.AreEqual (ImageOrientation.None, tag.Orientation, "Orientation");
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/JpegNikon1Bibble5Test.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/JpegNikon2Bibble5Test.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TaglibSharp.Tests/Images/JpegOlympus2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void CheckXMP (File file)

Assert.IsNotNull (tag, "tag");

Assert.AreEqual (new string[] { }, tag.Keywords);
Assert.AreEqual (Array.Empty<string> (), tag.Keywords);
Assert.AreEqual ("OLYMPUS CORPORATION", tag.Make);
Assert.AreEqual ("C5060WZ", tag.Model);
Assert.AreEqual ("Adobe Photoshop Elements 4.0", tag.Software);
Expand Down
2 changes: 1 addition & 1 deletion src/TaglibSharp.Tests/Images/PefPentaxKd10Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void ValidateMetadataInvariants (TagLib.Image.File file)
var imagetag = file.ImageTag;
Assert.IsNotNull (imagetag);
Assert.AreEqual (String.Empty, imagetag.Comment, "Comment");
Assert.AreEqual (new string[] { }, imagetag.Keywords, "Keywords");
Assert.AreEqual (Array.Empty<string> (), imagetag.Keywords, "Keywords");
Assert.AreEqual (null, imagetag.Rating, "Rating");
Assert.AreEqual (TagLib.Image.ImageOrientation.LeftBottom, imagetag.Orientation, "Orientation");
Assert.AreEqual ("K10D Ver 1.31 ", imagetag.Software, "Software");
Expand Down
2 changes: 1 addition & 1 deletion src/TaglibSharp.Tests/Images/RawLeicaDigilux2Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void ValidateMetadataInvariants (TagLib.Image.File file)
var imagetag = file.ImageTag;
Assert.IsNotNull (imagetag);
Assert.AreEqual (String.Empty, imagetag.Comment, "Comment");
Assert.AreEqual (new string[] { }, imagetag.Keywords, "Keywords");
Assert.AreEqual (Array.Empty<string> (), imagetag.Keywords, "Keywords");
Assert.AreEqual (null, imagetag.Rating, "Rating");
Assert.AreEqual (TagLib.Image.ImageOrientation.TopLeft, imagetag.Orientation, "Orientation");
Assert.AreEqual (null, imagetag.Software, "Software");
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/RecursiveIFDTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand Down Expand Up @@ -259,7 +261,7 @@ public void ValidateMetadataInvariants (TagLib.Image.File file)
Assert.IsNotNull (entry, "Entry 0x92D6 missing in IFD 0");
Assert.IsNotNull (entry as UndefinedIFDEntry, "Entry is not an undefined IFD entry!");
var parsed_bytes = (entry as UndefinedIFDEntry).Data.Data;
var bytes = new byte[] { };
var bytes = Array.Empty<byte> ();
Assert.AreEqual (bytes, parsed_bytes);
}
// Thumbnail.0x0103 (Compression/Short/1) "6"
Expand Down
2 changes: 1 addition & 1 deletion src/TaglibSharp.Tests/Images/Rw2PanasonicG1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ValidateMetadataInvariants (TagLib.Image.File file)
var imagetag = file.ImageTag;
Assert.IsNotNull (imagetag);
Assert.AreEqual (String.Empty, imagetag.Comment, "Comment");
Assert.AreEqual (new string[] { }, imagetag.Keywords, "Keywords");
Assert.AreEqual (Array.Empty<string> (), imagetag.Keywords, "Keywords");
Assert.AreEqual (null, imagetag.Rating, "Rating");
Assert.AreEqual (TagLib.Image.ImageOrientation.TopLeft, imagetag.Orientation, "Orientation");
Assert.AreEqual (null, imagetag.Software, "Software");
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/TiffCanonBibble516BitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/TiffCanonBibble58BitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/TiffNikon1Bibble516BitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/TiffNikon1Bibble58BitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/TiffNikon2Bibble516BitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/TaglibSharp.Tests/Images/TiffNikon2Bibble58BitTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using TagLib.IFD;
Expand All @@ -20,7 +22,7 @@ public void Test ()
new CommentModificationValidator (string.Empty),
new TagCommentModificationValidator (null, TagTypes.TiffIFD, true),
new TagCommentModificationValidator (null, TagTypes.XMP, true),
new TagKeywordsModificationValidator (new string[] { }, TagTypes.XMP, true)
new TagKeywordsModificationValidator (Array.Empty<string> (), TagTypes.XMP, true)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;

Expand All @@ -12,7 +14,7 @@ public class KeywordsModificationValidator : IMetadataModificationValidator
readonly string[] orig_keywords;
readonly string[] test_keywords = { "keyword 1", "§$&§%", "99 dsf", "ഈ ヰᛥกツ" };

public KeywordsModificationValidator () : this (new string[] { }) { }
public KeywordsModificationValidator () : this (Array.Empty<string> ()) { }

public KeywordsModificationValidator (string[] orig_keywords)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;

Expand All @@ -12,7 +14,7 @@ public class TagKeywordsModificationValidator : KeywordsModificationValidator
readonly bool tag_present;
readonly TagTypes type;

public TagKeywordsModificationValidator (TagTypes type, bool tag_present) : this (new string[] { }, type, tag_present) { }
public TagKeywordsModificationValidator (TagTypes type, bool tag_present) : this (Array.Empty<string> (), type, tag_present) { }

public TagKeywordsModificationValidator (string[] orig_keywords, TagTypes type, bool tag_present) : base (orig_keywords)
{
Expand Down
12 changes: 7 additions & 5 deletions src/TaglibSharp.Tests/TaggingFormats/ApeTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using Tag = TagLib.Ape.Tag;
Expand Down Expand Up @@ -61,7 +63,7 @@ public void TestPerformers ()
}
});

tag.Performers = new string[0];
tag.Performers = Array.Empty<string> ();

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -89,7 +91,7 @@ public void TestAlbumArtists ()
}
});

tag.AlbumArtists = new string[0];
tag.AlbumArtists = Array.Empty<string> ();

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -117,7 +119,7 @@ public void TestComposers ()
}
});

tag.Composers = new string[0];
tag.Composers = Array.Empty<string> ();

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -205,7 +207,7 @@ public void TestGenres ()
}
});

tag.Genres = new string[0];
tag.Genres = Array.Empty<string> ();

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -501,7 +503,7 @@ public void TestPictures ()
}
});

tag.Pictures = new Picture[0];
tag.Pictures = Array.Empty<Picture> ();

TagTestWithSave (ref tag, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down
12 changes: 7 additions & 5 deletions src/TaglibSharp.Tests/TaggingFormats/AsfTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using NUnit.Framework;
using TagLib;
using File = TagLib.Asf.File;
Expand Down Expand Up @@ -60,7 +62,7 @@ public void TestPerformers ()
}
});

file.Tag.Performers = new string[0];
file.Tag.Performers = Array.Empty<string> ();

TagTestWithSave (ref file, abst, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -88,7 +90,7 @@ public void TestAlbumArtists ()
}
});

file.Tag.AlbumArtists = new string[0];
file.Tag.AlbumArtists = Array.Empty<string> ();

TagTestWithSave (ref file, abst, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -116,7 +118,7 @@ public void TestComposers ()
}
});

file.Tag.Composers = new string[0];
file.Tag.Composers = Array.Empty<string> ();

TagTestWithSave (ref file, abst, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -204,7 +206,7 @@ public void TestGenres ()
}
});

file.Tag.Genres = new string[0];
file.Tag.Genres = Array.Empty<string> ();

TagTestWithSave (ref file, abst, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down Expand Up @@ -500,7 +502,7 @@ public void TestPictures ()
}
});

file.Tag.Pictures = new Picture[0];
file.Tag.Pictures = Array.Empty<Picture> ();

TagTestWithSave (ref file, abst, delegate (Tag t, string m) {
Assert.IsTrue (t.IsEmpty, "Value Cleared (IsEmpty): " + m);
Expand Down
Loading

0 comments on commit 55c816c

Please sign in to comment.