小编jun*_*iro的帖子

在C#中测试私有方法的单元

Visual Studio允许通过自动生成的访问器类对私有方法进行单元测试.我编写了一个成功编译的私有方法的测试,但它在运行时失败了.一个相当小的代码和测试版本是:

//in project MyProj
class TypeA
{
    private List<TypeB> myList = new List<TypeB>();

    private class TypeB
    {
        public TypeB()
        {
        }
    }

    public TypeA()
    {
    }

    private void MyFunc()
    {
        //processing of myList that changes state of instance
    }
}    

//in project TestMyProj           
public void MyFuncTest()
{
    TypeA_Accessor target = new TypeA_Accessor();
    //following line is the one that throws exception
    target.myList.Add(new TypeA_Accessor.TypeB());
    target.MyFunc();

    //check changed state of target
}
Run Code Online (Sandbox Code Playgroud)

运行时错误是:

Object of type System.Collections.Generic.List`1[MyProj.TypeA.TypeA_Accessor+TypeB]' cannot be converted to type …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing

265
推荐指数
10
解决办法
19万
查看次数

Marshal.StructureToPtr失败了bool和固定大小的数组?

如果我用这个结构StructureToPtr编组,然后再用它解组PtrToStructure,我的第一个节点有y = {1,2},而我的第二个节点有y = {1,0}.

我不知道为什么,也许我的结构在某种程度上是坏的?bool从结构中删除它使它工作.

using System;
using System.Runtime.InteropServices;

namespace csharp_test
{
    unsafe class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct Node
        {
            public bool boolVar;
            public fixed int y[2];
        }

        unsafe static void Main(string[] args)
        {
            Node node = new Node();

            node.y[0] = 1;
            node.y[1] = 2;
            node.boolVar = true;

            int size = sizeof(Node);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(node, ptr, false);
            Node node2 = (Node)Marshal.PtrToStructure(ptr, typeof(Node));
            Marshal.FreeHGlobal(ptr);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# marshalling

8
推荐指数
1
解决办法
4689
查看次数

F#中的匿名函数和重载方法

因此,如果要在F#中向控制台写出一行,请执行以下操作:

System.Console.WriteLine "foo"
Run Code Online (Sandbox Code Playgroud)

最初我认为以下几乎完全相同,只是更详细,但实际上它给出了错误"方法'WriteLine'的唯一重载无法根据此程序点之前的类型信息确定":

(fun line -> System.Console.WriteLine line) "foo"
Run Code Online (Sandbox Code Playgroud)

似乎第二个版本因为重载的WriteLine方法的存在而感到困惑,这些方法接受字符串以及其他参数.我的假设是正确的吗?

f#

5
推荐指数
1
解决办法
423
查看次数

使用F#循环vs递归

这里的示例代码解决了项目Euler问题:

从数字1开始并沿顺时针方向向右移动,形成5乘5螺旋,如下所示:

 21 22 23 24 25 
 20  7  8  9 10  
 19  6  1  2 11    
 18  5  4  3 12 
 17 16 15 14 13
Run Code Online (Sandbox Code Playgroud)

可以验证对角线上的数字之和为101.

以相同方式形成的1001乘1001螺旋中对角线上的数字总和是多少?

但我的问题是函数式编程风格而不是如何得到答案(我已经拥有它).我试图通过避免我的解决方案中的命令性循环来教自己一些关于函数式编程的知识,因此提出了以下递归函数来解决问题28:

let answer = 
    let dimensions = 1001
    let max_number = dimensions * dimensions

    let rec loop total increment increment_count current =
        if current > max_number then total
        else
            let new_inc, new_inc_count =
                if increment_count = 4 then increment + 2, 0
                else increment, increment_count + 1
            loop (total + current) …
Run Code Online (Sandbox Code Playgroud)

recursion f#

4
推荐指数
1
解决办法
1402
查看次数

从F#调用重载的C#方法

在SO上有一些类似的问题,但我似乎无法找到我正在寻找的东西.

有一个C#库(OpenCVSharp)声明一个像这样的重载方法:

public static void CalcHist(Mat[] images, 
            int[] channels, InputArray mask,
            OutputArray hist, int dims, int[] histSize,
            Rangef[] ranges, bool uniform = true, bool accumulate = false)
{
           ....
}

public static void CalcHist(Mat[] images,
            int[] channels, InputArray mask,
            OutputArray hist, int dims, int[] histSize,
            float[][] ranges, bool uniform = true, bool accumulate = false)
{
    ....
}
Run Code Online (Sandbox Code Playgroud)

即仅仅通过"范围"参数的类型来改变.

我似乎无法调用此方法,即使使用tupled参数样式,包括可选参数和添加一大堆类型注释:

let images = [|new Mat()|] 
let hist = OutputArray.Create(new Mat());
let hdims = [|256|];
let ranges = [| new Rangef(0.f,256.f) …
Run Code Online (Sandbox Code Playgroud)

c# f#

4
推荐指数
1
解决办法
489
查看次数

意外地在F#interactive中重新定义了int64

也许我错过了一些明显的东西.在F#解释器中,有些东西像我期望的那样工作:

> 10;;
val it : int = 10
> int 10;;
val it : int = 10
> 10L;;
val it : int64 = 10L
Run Code Online (Sandbox Code Playgroud)

但是,有些事情没有.我希望以下内容返回int64,而不是int,我不希望10转换为0.

> int64 10;;
val it : int = 0
> int64;;
val it : ('a -> int) = <fun:clo@143-3>
Run Code Online (Sandbox Code Playgroud)

如何将整数转换为int64?

f#

3
推荐指数
1
解决办法
1268
查看次数

如何将concat与terraform中的formatlist结合起来?

是否可以连接formatlist生成的列表?以下给出了错误

At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 6 is TypeList):

{
    "Action": [
        "s3:Get*",
        "s3:List*"
    ],
   "Effect": "Allow",
   "Resource": ["${concat(
         formatlist("arn:aws:s3:::%s", ${var.data_pipeline_s3_buckets}),
         formatlist("arn:aws:s3:::%s/*", ${var.data_pipeline_s3_buckets}))}"]
},
Run Code Online (Sandbox Code Playgroud)

terraform

3
推荐指数
1
解决办法
4224
查看次数

如何将包含数组列的Dask数据框写入拼花文件

我有一个Dask数据框,其中一列包含一个浮点数的numpy数组:

import dask.dataframe as dd
import pandas as pd
import numpy as np

df = dd.from_pandas(
    pd.DataFrame(
        {
            'id':range(1, 6),
            'vec':[np.array([1.0, 2.0, 3.0, 4.0, 5.0])] * 5
        }), npartitions=1)

df.compute()

   id                        vec
0   1  [1.0, 2.0, 3.0, 4.0, 5.0]
1   2  [1.0, 2.0, 3.0, 4.0, 5.0]
2   3  [1.0, 2.0, 3.0, 4.0, 5.0]
3   4  [1.0, 2.0, 3.0, 4.0, 5.0]
4   5  [1.0, 2.0, 3.0, 4.0, 5.0]
Run Code Online (Sandbox Code Playgroud)

如果我尝试将其写为实木复合地板,则会出现错误:

df.to_parquet('somefile')
....
Error converting column "vec" to bytes using encoding UTF8. Original …
Run Code Online (Sandbox Code Playgroud)

python dask fastparquet

3
推荐指数
1
解决办法
608
查看次数

标签 统计

f# ×4

c# ×3

dask ×1

fastparquet ×1

marshalling ×1

python ×1

recursion ×1

terraform ×1

unit-testing ×1