Repository: microsoft/garnet
File: test/Garnet.test/RespSortedSetTests.cs (C#)

Code before:
            var result2 = (long)db.Execute("ZINTERSTORE", "dest", "2", "zset1", "zset2", "AGGREGATE", "MAX");
            ClassicAssert.AreEqual(2, result2);

            storedValues = db.SortedSetRangeByScoreWithScores("dest");
            ClassicAssert.AreEqual(2, storedValues.Length);
            ClassicAssert.AreEqual(1, storedValues[0].Score); // MAX of scores
            ClassicAssert.AreEqual(2, storedValues[1].Score); // MAX of scores

            // Test error cases
            var ex = Assert.Throws<RedisServerException>(() => db.Execute("ZINTERSTORE", "dest"));
            ClassicAssert.AreEqual(string.Format(CmdStrings.GenericErrWrongNumArgs, "ZINTERSTORE"), ex.Message);
        }

        [Test]
        [TestCase("SUM", new double[] { 3, 5, 6, 7 }, new string[] { "c", "a", "d", "b" }, Description = "Tests ZUNION with SUM aggregate")]
        [TestCase("MIN", new double[] { 1, 2, 3, 6 }, new string[] { "a", "b", "c", "d" }, Description = "Tests ZUNION with MIN aggregate")]
        [TestCase("MAX", new double[] { 3, 4, 5, 6 }, new string[] { "c", "a", "b", "d" }, Description = "Tests ZUNION with MAX aggregate")]
        public void CanUseZUnionWithAggregateOption(string aggregateType, double[] expectedScores, string[] expectedElements)
        {
            using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
            var db = redis.GetDatabase(0);

            // Setup test data
            db.SortedSetAdd("zset1",
            [
                new SortedSetEntry("a", 1),
                new SortedSetEntry("b", 2),
                new SortedSetEntry("c", 3)
            ]);
            db.SortedSetAdd("zset2",
            [
                new SortedSetEntry("a", 4),
                new SortedSetEntry("b", 5),
                new SortedSetEntry("d", 6)
            ]);

            var result = db.SortedSetCombineWithScores(SetOperation.Union, ["zset1", "zset2"],
                weights: null, aggregate: aggregateType switch
                {
                    "SUM" => Aggregate.Sum,
                    "MIN" => Aggregate.Min,
                    "MAX" => Aggregate.Max,
                    _ => throw new ArgumentException("Invalid aggregate type")
                });

            ClassicAssert.AreEqual(expectedScores.Length, result.Length);
            for (int i = 0; i < result.Length; i++)
            {
                ClassicAssert.AreEqual(expectedScores[i], result[i].Score);
                ClassicAssert.AreEqual(expectedElements[i], result[i].Element.ToString());
            }

Review comment (suggestion):
Since there are no test cases covering the ordering of ZINTER / ZDIFF's outputs,
I'd say add a couple of elements to the sets in `CanDoZInterWithSE` & `CanDoZDiff` so that we can verify the ordering...

Code after:
            var weights = new double[] { 2, 3 };
            result = db.SortedSetCombineAndStore(SetOperation.Intersect, "dest", [new RedisKey("zset1"), new RedisKey("zset2")], weights);
            ClassicAssert.AreEqual(2, result);

            storedValues = db.SortedSetRangeByScoreWithScores("dest");
            ClassicAssert.AreEqual(2, storedValues.Length);
            ClassicAssert.AreEqual("one", storedValues[0].Element.ToString());
            ClassicAssert.AreEqual(5, storedValues[0].Score); // Weighted sum
            ClassicAssert.AreEqual("two", storedValues[1].Element.ToString());
            ClassicAssert.AreEqual(10, storedValues[1].Score); // Weighted sum

            // Test with MAX aggregate
            var result2 = (long)db.Execute("ZINTERSTORE", "dest", "2", "zset1", "zset2", "AGGREGATE", "MAX");
            ClassicAssert.AreEqual(2, result2);

            storedValues = db.SortedSetRangeByScoreWithScores("dest");
            ClassicAssert.AreEqual(2, storedValues.Length);
            ClassicAssert.AreEqual(1, storedValues[0].Score); // MAX of scores
            ClassicAssert.AreEqual(2, storedValues[1].Score); // MAX of scores

            // Test error cases
            var ex = Assert.Throws<RedisServerException>(() => db.Execute("ZINTERSTORE", "dest"));
            ClassicAssert.AreEqual(string.Format(CmdStrings.GenericErrWrongNumArgs, "ZINTERSTORE"), ex.Message);
        }

        [Test]
        [TestCase("SUM", new double[] { 3, 5, 6, 7 }, new string[] { "c", "a", "d", "b" }, Description = "Tests ZUNION with SUM aggregate")]
        [TestCase("MIN", new double[] { 1, 2, 3, 6 }, new string[] { "a", "b", "c", "d" }, Description = "Tests ZUNION with MIN aggregate")]
        [TestCase("MAX", new double[] { 3, 4, 5, 6 }, new string[] { "c", "a", "b", "d" }, Description = "Tests ZUNION with MAX aggregate")]
        public void CanUseZUnionWithAggregateOption(string aggregateType, double[] expectedScores, string[] expectedElements)
        {
            using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
            var db = redis.GetDatabase(0);

            // Setup test data
            db.SortedSetAdd("zset1",
            [
                new SortedSetEntry("a", 1),
                new SortedSetEntry("b", 2),
                new SortedSetEntry("c", 3)
            ]);
            db.SortedSetAdd("zset2",
            [
                new SortedSetEntry("a", 4),
                new SortedSetEntry("b", 5),
                new SortedSetEntry("d", 6)
            ]);

            var result = db.SortedSetCombineWithScores(SetOperation.Union, ["zset1", "zset2"],
                weights: null, aggregate: aggregateType switch
                {