Using Moq4, I am trying to replace one of the method's argument as done with a List<string> on this post. However, using the byte[] type, I am not able to change the value. Any idea or solution?
The code
public class SomeObject
{
public virtual void DoSomething(byte[] array, int offset, int count) { }
}
[Fact]
public void SomeTest()
{
// Arrange
byte[] expectedArray = new byte[] { 5, 6, 7, 8, 9 };
var mock = new Mock<SomeObject>();
mock.Setup(so => so.DoSomething(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
.Callback<byte[], int, int>(
(buffer, offset, count) => { buffer = new byte[] { 5, 6, 7, 8, 9 }; }
);
var target = mock.Object;
var array = new byte[64];
// Act
target.DoSomething(array, 0, 10);
// Assert
Assert.Equal(expectedArray, array);
}
Obtained output
Message:
Assert.Equal() Failure
Expected: Byte[] [5, 6, 7, 8, 9]
Actual: Byte[] [0, 0, 0, 0, 0, ...]
Assigning new byte[] { 5, 6, 7, 8, 9 }; to the lambda parameter creates a new object, which is different than the original byte[64] that you created beforehand.
The only way your lambda can modify the values passed by reference is using that reference, not replacing it. i.e: buffer[0] = 5.
If SomeObject is your own code (and you should not mock what you don't own), consider a signature that returns the array instead, in line with CQS. You could then just use .Returns() in this case.
Related
Code given below is giving error that at b[3] cannot convert from int to string
static void Main(string[] args)
{
int[] b = new int[5] { 1, 7, 8, 9, 2 };
Console.WriteLine(b[3],b[4]);
}
whereas code given below is working properly without any issue
int[] b = new int[5] { 1, 7, 8, 9, 2 };
Console.WriteLine($"{b[3]} {b[4]}");
Console.WriteLine is not well suited to just print out passed multiple arguments (as print in python does, for example). Console.WriteLine overloads with 2 parameters (1, 2) require first parameter to be a string containing a composite format string and the second parameter will be used to fill placheholders in this format string.
If you want to print out several items you need either combine them into string manually. For example:
via string inteerpolation as in your question
Console.WriteLine($"{b[3]} {b[4]}");
using string.Join (more convenient with collections):
Console.WriteLine(string.Join(" ", new []{ b[3], b[4]}));
or use the format string:
Console.WriteLine("{0} {1}", b[3], b[4]);
or
Console.WriteLine("{0} {1}", new object[]{b[3], b[4]}); // better used with reference types
Console.WriteLine expects one parameter to print. You are passing 2 parameters.
You can change to:
static void Main(string[] args)
{
int[] b = new int[5] { 1, 7, 8, 9, 2 };
Console.WriteLine(b[3]);
Console.WriteLine(b[4]);
}
Or better:
static void Main(string[] args)
{
int[] b = new int[5] { 1, 7, 8, 9, 2 };
Console.WriteLine($"{b[3]} {b[4]}");
}
In this case I pass one parameter with string interpolation.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need a method to return the select elements in a 1D array where the the position of the elements are stored in another array as follow:
double[] A = new double[11] { 8, 9, 8, 7, 5, 6, 4, 8, 9, 6, 5};
Int32[] C = new Int32[3] { 1, 5, 8};
double[] B = MyMethod(A, C);
It should return:
{9, 6, 9}
I'm lost in Linq's Select, Where, Take :-)
With linq, all you would need to do is Select from the Offset array of int and project those int elements into the indexer of the source array
Here is a generic solution that will work with any type
Given
public static T[] SliceAndDice<T>(T[] source, int[] offsets)
=> offsets.Select(t => source[t]).ToArray();
Usage
var a = new double[11] { 8, 9, 8, 7, 5, 6, 4, 8, 9, 6, 5 };
var b = new int[3] { 1, 5, 8 };
var results = SliceAndDice(a,b);
Console.WriteLine(string.Join(", ", results));
Output
9, 6, 9
If you need the brackets
Console.WriteLine($"{{{string.Join(", ", results)}}}");
Output
{9, 6, 9}
For a completely validated and checked extension method
public static T[] SliceAndDice<T>(this T[] source, int[] offsets)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (offsets == null) throw new ArgumentNullException(nameof(offsets));
var result = new T[offsets.Length];
for (var i = 0; i < offsets.Length; i++)
{
if(offsets[i] >= source.Length)
throw new IndexOutOfRangeException("Index outside the bounds of the source array");
result[i] = source[offsets[i]];
}
return result;
}
This is very similar to the other answer, just stripped down to the bare minimum for an extension method.
static class Program
{
static void Main(string[] args)
{
double[] A = new double[] { 8, 9, 8, 7, 5, 6, 4, 8, 9, 6, 5 };
double[] B = A.Slice(new[] { 1, 5, 8 } );
}
public static T[] Slice<T>(this T[] array, IEnumerable<int> index)
{
return index.Select((i) => array[i]).ToArray();
}
}
You can use overload of LINQ Where extension method which uses index:
var x = A.Where((z, index) => C.Any(j => j == index));
Looking for the most efficient way. I found this on comparing lists regardless of order:
https://answers.unity.com/questions/1307074/how-do-i-compare-two-lists-for-equality-not-caring.html
What about comparing array contents regardless of order?
You can use the Intersect method. Here is a simple console application
using System;
using System.Linq;
class Program
{
static void Main()
{
var nums1 = new int[] { 2, 4, 6, 8, 10, 9 };
var nums2 = new int[] { 1, 3, 6, 9, 12, 2 };
if (nums1.Intersect(nums2).Any()) // check if there is equal items
{
var equalItems = nums1.Intersect(nums2); // get list of equal items (2, 6, 9)
// ...
}
}
}
I'm having issue with the following code:
public int ReverseArray(int[] rArray)
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
Array.Reverse(array);
foreach (int value in array)
{
return (value);
}
return 0;
}
private void reverseButton_Click(object sender, EventArgs e)
{
int[] input = new int[10];
int output = ReverseArray(input);
MessageBox.Show(""+ output);
}
The code is supposed to take the given array (int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };) and reverse it upon a button click; however, when I click on the reverseButton, I only get the number 8 and not the entire array. I'm sure it's the way my reversButton code is written, but I'm not sure how to fix it.
How can I fix my code to where when I click on reverseButton, the entire array will be displayed in reverse order?
You are not returning whole array back and also not iterating the array result and Array is a collection of items to you need to tell it to get each item one by one and print.
You can do something like following to make it work:
public int[] ReverseArray(int[] rArray)
{
Array.Reverse(array);
return array;
}
and in button click event you can use it:
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8 };
input = ReverseArray(input);
string items = String.Join(",",input);
MessageBox.Show(items);
Because your method ReverseArray just returns one value:
public int[] ReverseArray(int[] rArray)
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
Array.Reverse(array);
return array;
}
To show whole array you can combine it into some string:
int[] output = ReverseArray(input);
var message = string.Join(", ", output);
And show:
MessageBox.Show(message);
this might sound unclear but i know its difficult but we can remove specific bytes from byte[] array but if the array contain similiar values while removing values it can remove other values i m using :
byte[] B = new byte[] { 10, 0, 0, 10 };
byte[] D = new byte[] { 0, 0 };
byte[] NewArray = B.Except(D).ToArray();
BytesDisplayer.Text = String.Join(",", NewArray);
but lets say i have a byte as:
byte[] Data = new byte[] {0,10,10,10,0,5,5,5,10,10,10};
and i want to remove the last 3 values (10) My method will remove all the 10 value on that array , so basically i want to know is there is a way to remove specific bytes in specific indexes and how ?
try this:
byte[] Data = new byte[] { 0, 10, 10, 10, 0, 5, 5, 5, 10, 10, 10 };
Data = Data.Where((item, index) => index < 8).ToArray();