-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new sum method on spans to better avoid collisions (#68)
* Add `ReadOnlySpan<byte>.MSum()` * Irc: Changed internal tag values to match `ReadOnlySpan<byte>.MSum()` rather than `.Sum()` * PubSub: Switch to MSum() * Add some MSum() tests for irc * Sum tests for pubsub * remove obsolete test * use actual MSum()
- Loading branch information
Showing
20 changed files
with
373 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
using System.Drawing; | ||
using System.Globalization; | ||
using MiniTwitch.Irc.Enums; | ||
using MiniTwitch.Irc.Enums; | ||
using MiniTwitch.Irc.Models; | ||
using Xunit; | ||
|
||
|
@@ -184,64 +182,6 @@ public void WithNestedReply() | |
Assert.Equal(1687870580760, privmsg.TmiSentTs); | ||
} | ||
|
||
[Fact] | ||
public void WithHypeChat() | ||
{ | ||
var privmsg = Privmsg.Construct("@badge-info=subscriber/28;badges=subscriber/24,chatter-cs-go-2022/1;color=#00FFFF;display-name=MoodyJ1;emotes=;first-msg=0;flags=;id=ad4517d0-97b7-4ca1-91eb-f43edf3bf9b3;mod=0;pinned-chat-paid-amount=100;pinned-chat-paid-canonical-amount=100;pinned-chat-paid-currency=GBP;pinned-chat-paid-exponent=2;pinned-chat-paid-is-system-message=0;pinned-chat-paid-level=ONE;returning-chatter=0;room-id=22484632;subscriber=1;tmi-sent-ts=1687470141956;turbo=0;user-id=246651933;user-type= :[email protected] PRIVMSG #forsen :f o r s e n"); | ||
|
||
// Author | ||
Assert.Equal(246651933, privmsg.Author.Id); | ||
Assert.Equal(UserType.None, privmsg.Author.Type); | ||
Assert.True(privmsg.Author.IsSubscriber); | ||
Assert.Equal("MoodyJ1", privmsg.Author.DisplayName); | ||
Assert.Equal("moodyj1", privmsg.Author.Name); | ||
Assert.False(privmsg.Author.IsMod); | ||
Assert.False(privmsg.Author.IsTurbo); | ||
Assert.Equal(Color.FromArgb(int.Parse("00FFFF", NumberStyles.HexNumber)).Name, privmsg.Author.ChatColor.Name); | ||
Assert.Equal("subscriber/28", privmsg.Author.BadgeInfo); | ||
Assert.Equal("subscriber/24,chatter-cs-go-2022/1", privmsg.Author.Badges); | ||
Assert.False(privmsg.Author.IsVip); | ||
|
||
// No reply | ||
Assert.False(privmsg.Reply.HasContent); | ||
Assert.Equal(string.Empty, privmsg.Reply.ParentMessage); | ||
Assert.Equal(string.Empty, privmsg.Reply.ParentDisplayName); | ||
Assert.Equal(string.Empty, privmsg.Reply.ParentUsername); | ||
Assert.Equal(string.Empty, privmsg.Reply.ParentMessageId); | ||
Assert.Equal(0, privmsg.Reply.ParentUserId); | ||
Assert.Equal(string.Empty, privmsg.Reply.ParentThreadMessageId); | ||
Assert.Equal(string.Empty, privmsg.Reply.ParentThreadUsername); | ||
|
||
// Is hypechat message | ||
Assert.True(privmsg.HypeChat.HasContent); | ||
Assert.Equal(HypeChatLevel.ONE, privmsg.HypeChat.Level); | ||
Assert.Equal(CurrencyCode.GBP, privmsg.HypeChat.Currency); | ||
Assert.Equal(2, privmsg.HypeChat.Exponent); | ||
Assert.Equal(100, privmsg.HypeChat.PaidAmount); | ||
Assert.False(privmsg.HypeChat.IsSystemMessage); | ||
Assert.Equal(1, privmsg.HypeChat.GetActualAmount()); | ||
Assert.Equal(TimeSpan.FromSeconds(30), privmsg.HypeChat.GetPinDuration()); | ||
|
||
// Channel | ||
Assert.Equal("forsen", privmsg.Channel.Name); | ||
Assert.Equal(22484632, privmsg.Channel.Id); | ||
|
||
// No source | ||
Assert.Null(privmsg.Source); | ||
Assert.True(privmsg.ReplyWith(default!, default!).IsCompleted); | ||
|
||
Assert.Equal(0, privmsg.Bits); | ||
Assert.Equal("f o r s e n", privmsg.Content); | ||
Assert.Equal(string.Empty, privmsg.Emotes); | ||
Assert.Equal(string.Empty, privmsg.Flags); | ||
Assert.Equal("ad4517d0-97b7-4ca1-91eb-f43edf3bf9b3", privmsg.Id); | ||
Assert.False(privmsg.IsAction); | ||
Assert.False(privmsg.IsFirstMessage); | ||
Assert.False(privmsg.IsReturningChatter); | ||
Assert.Equal(string.Empty, privmsg.Nonce); | ||
Assert.Equal(1687470141956, privmsg.TmiSentTs); | ||
} | ||
|
||
[Fact] | ||
public void WithBits() | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Text; | ||
using MiniTwitch.Common.Extensions; | ||
using MiniTwitch.Irc.Internal.Enums; | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace MiniTwitch.Irc.Test; | ||
public class SpanSumTests | ||
{ | ||
// <expected, string> | ||
static readonly Dictionary<int, string> _values = new() | ||
{ | ||
{ (int)Tags.Id, "id" }, | ||
{ (int)Tags.Mod, "mod" }, | ||
{ (int)Tags.Vip, "vip" }, | ||
{ (int)Tags.R9K, "r9k" }, | ||
{ (int)Tags.Bits, "bits" }, | ||
{ (int)Tags.Flags, "flags" }, | ||
{ (int)Tags.Color, "color" }, | ||
{ (int)Tags.Slow, "slow" }, | ||
{ (int)Tags.Turbo, "turbo" }, | ||
{ (int)Tags.Badges, "badges" }, | ||
{ (int)Tags.Emotes, "emotes" }, | ||
{ (int)Tags.UserId, "user-id" }, | ||
{ (int)Tags.FirstMsg, "first-msg" }, | ||
{ (int)Tags.MsgId, "msg-id" }, | ||
{ (int)Tags.Login, "login" }, | ||
{ (int)Tags.RoomId, "room-id" }, | ||
{ (int)Tags.SubsOnly, "subs-only" }, | ||
{ (int)Tags.EmoteOnly, "emote-only" }, | ||
{ (int)Tags.TmiSentTs, "tmi-sent-ts" }, | ||
{ (int)Tags.BanDuration, "ban-duration" }, | ||
{ (int)Tags.TargetMsgId, "target-msg-id" }, | ||
{ (int)Tags.TargetUserId, "target-user-id" }, | ||
{ (int)Tags.FollowersOnly, "followers-only" }, | ||
{ (int)Tags.UserType, "user-type" }, | ||
{ (int)Tags.BadgeInfo, "badge-info" }, | ||
{ (int)Tags.Subscriber, "subscriber" }, | ||
{ (int)Tags.ClientNonce, "client-nonce" }, | ||
{ (int)Tags.DisplayName, "display-name" }, | ||
{ (int)Tags.ReturningChatter, "returning-chatter" }, | ||
{ (int)Tags.ReplyParentMsgId, "reply-parent-msg-id" }, | ||
{ (int)Tags.ReplyParentUserId, "reply-parent-user-id" }, | ||
{ (int)Tags.ReplyParentMsgBody, "reply-parent-msg-body" }, | ||
{ (int)Tags.ReplyParentUserLogin, "reply-parent-user-login" }, | ||
{ (int)Tags.ReplyParentDisplayName, "reply-parent-display-name" }, | ||
{ (int)Tags.ReplyThreadParentMsgId, "reply-thread-parent-msg-id" }, | ||
{ (int)Tags.ReplyThreadParentUserLogin, "reply-thread-parent-user-login" }, | ||
{ (int)Tags.SystemMsg, "system-msg" }, | ||
{ (int)Tags.MsgParamColor, "msg-param-color" }, | ||
{ (int)Tags.MsgParamMonths, "msg-param-months" }, | ||
{ (int)Tags.MsgParamSubPlan, "msg-param-sub-plan" }, | ||
{ (int)Tags.MsgParamSenderName, "msg-param-sender-name" }, | ||
{ (int)Tags.MsgParamGiftMonths, "msg-param-gift-months" }, | ||
{ (int)Tags.MsgParamViewerCount, "msg-param-viewerCount" }, | ||
{ (int)Tags.MsgParamRecipientId, "msg-param-recipient-id" }, | ||
{ (int)Tags.MsgParamSenderLogin, "msg-param-sender-login" }, | ||
{ (int)Tags.MsgParamSenderCount, "msg-param-sender-count" }, | ||
{ (int)Tags.MsgParamSubPlanName, "msg-param-sub-plan-name" }, | ||
{ (int)Tags.MsgParamStreakMonths, "msg-param-streak-months" }, | ||
{ (int)Tags.MsgParamMassGiftCount, "msg-param-mass-gift-count" }, | ||
{ (int)Tags.MsgParamCumulativeMonths, "msg-param-cumulative-months" }, | ||
{ (int)Tags.MsgParamRecipientUserName, "msg-param-recipient-user-name" }, | ||
{ (int)Tags.MsgParamShouldShareStreak, "msg-param-should-share-streak" }, | ||
{ (int)Tags.MsgParamRecipientDisplayName, "msg-param-recipient-display-name" }, | ||
{ (int)Tags.MsgParamCharityName, "msg-param-charity-name" }, | ||
{ (int)Tags.MsgParamDonationAmount, "msg-param-donation-amount" }, | ||
{ (int)Tags.MsgParamExponent, "msg-param-exponent" }, | ||
{ (int)Tags.MsgParamDonationCurrency, "msg-param-donation-currency" }, | ||
{ (int)Tags.EmoteSets, "emote-sets" }, | ||
{ (int)Tags.MessageId, "message-id" }, | ||
{ (int)Tags.ThreadId, "thread-id" }, | ||
}; | ||
|
||
[Fact] | ||
public void Test_Includes_All_Tags() | ||
{ | ||
var tags = Enum.GetNames(typeof(Tags)); | ||
if (tags.Length != _values.Count) | ||
{ | ||
var notIncluded = tags.Where(x => !_values.ContainsKey((int)Enum.Parse<Tags>(x))); | ||
Assert.Fail($"Test does not account for all tags, expected {tags.Length}, got {_values.Count}." + | ||
$"\nMissing tags: \n{string.Join("\n", notIncluded)}"); | ||
} | ||
|
||
foreach (var tag in tags) | ||
{ | ||
var enumValue = Enum.Parse<Tags>(tag); | ||
try | ||
{ | ||
Assert.True(_values.ContainsKey((int)enumValue)); | ||
} | ||
catch (TrueException) | ||
{ | ||
Assert.Fail($"Enum value {enumValue} not present."); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void All_Tag_Values_Match_Sum() | ||
{ | ||
foreach (var kvp in _values) | ||
{ | ||
if (kvp.Key != MSum(kvp.Value)) | ||
{ | ||
Assert.Fail($"Tag \"{kvp.Value}\" does not match expected sum {kvp.Key}. Got {MSum(kvp.Value)} instead."); | ||
} | ||
} | ||
} | ||
|
||
|
||
private static int MSum(string s) | ||
{ | ||
var source = Encoding.UTF8.GetBytes(s); | ||
return SpanExtensions.MSum(source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.