标签: assertion

无法弄清楚为什么我的程序断言

我正在研究一个简单的向量程序作为一个赋值,但我无法弄清楚程序断言的原因.我的程序编译成功,但在运行时失败.我认为即时通讯在这方面的专业知识.

#include <iostream>
#include <cstring>
#include <assert.h>
#include <stdio.h>
#include <iomanip>
#define TESTING
using namespace std;
typedef float Elem;//floats for vector elements

struct Vector{//structure for the vector
    unsigned int size;
    Elem *svector;
};


int main(){

#ifdef TESTING
        //prototypes
    Vector *alloc_vec();
    bool print_vec(Vector *printVector);
    Vector *extend_vec(Vector *extend,Elem element);
    Vector *scalar_plus(Vector *vecToAdd, Elem addElement);
    void dealloc_vec(Vector *&deAlloc);

    //testing scaffolds
    Vector *testVec=new Vector;
    *testVec=*alloc_vec();
    assert(testVec->size==0);
    assert(testVec->svector==NULL);

    for(int i=0;i=10;i++){
        *testVec=*extend_vec(testVec,Elem(i));
    }

    assert(testVec->size!=0);
    assert(testVec->svector!=NULL);

    assert(print_vec(testVec));
    print_vec(testVec);

    *testVec=*scalar_plus(testVec,5);

    print_vec(testVec);

    dealloc_vec(testVec);

    assert(testVec==NULL);
#endif //testing

    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c memory assertion

0
推荐指数
1
解决办法
140
查看次数

断言:指针必须来自'本地'堆

我正在测试一个名为clunk的小型声音库(http://sourceforge.net/projects/clunk/).我为visual studio 11构建了该库,并将其链接到我的visual studio项目中.当我尝试test.cpp时,我收到了msvcr110d.dll抛出的断言.

是否与我的运行时库设置有关:它是" 多线程调试DLL(/ MDd) "?在clunk的cmakelist.txt中,我添加了以下代码行:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
Run Code Online (Sandbox Code Playgroud)

我仍然收到指针分配问题的消息.为什么?

c++ dll memory-management assertion

0
推荐指数
1
解决办法
1486
查看次数

python和AssertionError中的单元测试:False!= True

我正在python中进行一些单元测试.我在下面提到AssertionError.我想检查温度范围,如果它小于30且大于25,那么代码应该通过,但它给了我错误.我无法弄清楚我在哪里弄错了.

test_csv_read_data_headers (__main__.ParseCSVTest) ... ok
test_data_fuelConsumption (__main__.ParseCSVTest) ... ok
test_data_temperature (__main__.ParseCSVTest) ...FAIL
test_data_timestamp (__main__.ParseCSVTest) ... ok
Run Code Online (Sandbox Code Playgroud)

================================================== ====================

失败:test_data_temp( .ParseCSVTest)

Traceback (most recent call last):
File "try.py", line 36, in test_data_temperature
30 > ali > 25, True
AssertionError: False != True
Run Code Online (Sandbox Code Playgroud)
Ran 4 tests in 0.014s

FAILED (failures=1)
Run Code Online (Sandbox Code Playgroud)

我的测试失败的温度部分的代码如下.

def test_data_temperature(self):                
    column = [row[0].split()[3] for row in read_data(self.data)[1:]]                      
    ali = column[0:4]
    print ali                          
    self.assertEqual(
            30 > ali > 25, True 
            )
Run Code Online (Sandbox Code Playgroud)

我在ali中打印数据,它是以列表的形式

['25.8', '25.6', '25.8', '25.8']  
Run Code Online (Sandbox Code Playgroud)

我很困惑,我怎么能检查这个范围并做出断言,以便通过测试.如果有人给出提示或示例.我真的很感激.

python unit-testing assertion

0
推荐指数
1
解决办法
3633
查看次数

调用函数时,函数名和参数之间的“类型断言”是什么?

在这个Typescript React 入门指南中,它给出了:

import { createStore } from 'redux';

interface StoreState {
    languageName: string;
    enthusiasmLevel: number;
}

function enthusiasm(state: StoreState, action: EnthusiasmAction): StoreState {
    // returns a StoreState
}
const store = createStore<StoreState>(enthusiasm, {
     enthusiasmLevel: 1,
     languageName: 'TypeScript',   
});
Run Code Online (Sandbox Code Playgroud)

这个断言在那里做什么?

我找不到定义此语法的位置,也无法“推断”它的含义。

assertion typescript

0
推荐指数
1
解决办法
989
查看次数

在Haskell中执行断言

假设我有一个计算两个数字之和的函数:

computeSum :: Int -> Int -> Int
computeSum x y = x + y
Run Code Online (Sandbox Code Playgroud)

是否有任何形式的控制来自上述函数的返回值,我只想总结两个数字,其总和是非负数且必须小于10

我刚从命令式开始进行函数式编程,我们可以简单地检查函数返回值的命令式编程,例如:

if value <= 10 and value > 0:
   return value
Run Code Online (Sandbox Code Playgroud)

只是想知道在haskell中是否有类似的东西?

haskell assertion

0
推荐指数
1
解决办法
267
查看次数

如果断言通过,是否评估 Python 断言消息

假设我有一个assert包含大量计算错误消息的语句(例如,进行多个网络或数据库调用)。

assert x == 5, f"Some computationally heavy message here: {requests.get('xxx')}"
Run Code Online (Sandbox Code Playgroud)

我还可以使用 if 语句编写此代码:

if x != 5:
    raise AssertionError(f"Some computationally heavy message here: {requests.get('xxx')}")
Run Code Online (Sandbox Code Playgroud)

我知道后一个选项只会评估错误消息,如果x != 5. 前一种选择呢?我会这么认为,但我不确定。

python assert assertion

0
推荐指数
1
解决办法
43
查看次数

检查interface{}是否是struct的ptr

我想检查给定的f interface{}函数参数是否是指向结构的指针,但不知何故陷入困境:

更新的片段:

package main

import (
    "fmt"
    "log"
    "reflect"
)

func main() {

    // Switch f between being a pointer or not
    f := &struct{Foo string}{"Bar"}

    if err := something(f); err != nil {
        log.Fatal(err.Error())
    }

}

func something(f interface{}) error {

    if reflect.ValueOf(f).Kind() != reflect.Struct  {
        return fmt.Errorf("not struct; is %s", reflect.ValueOf(f).Kind().String())
    }

    if reflect.ValueOf(f).Kind() != reflect.Ptr  {
        return fmt.Errorf("not ptr; is %s", reflect.ValueOf(f).Kind().String())
    }

    // Deal with element values...
    t := reflect.ValueOf(f).Elem()

    for i := …
Run Code Online (Sandbox Code Playgroud)

reflection struct pointers go assertion

0
推荐指数
1
解决办法
2617
查看次数

静态检查越界

我有这个方法,它只获取成员的一个元素,这是一个 C 样式数组。

constexpr T get(const int&& idx) const {
    static_assert(idx >= sizeof(array) / sizeof(T));
    return array[idx];
}
Run Code Online (Sandbox Code Playgroud)

我想静态检查参数上的值,该值将尝试恢复超出范围的成员上的元素。因此,代码将拒绝编译。

我尝试使用静态断言,但是,显然:

function parameter 'idx' with unknown value cannot be used in a constant expression
Run Code Online (Sandbox Code Playgroud)

现代 C++ 实现这一目标的惯用方法是什么?可以在编译时检查吗?

如果不是,报告非法访问成员的开销较小的版本是什么?我想保持代码无异常。

编辑:

// call site example

decltype(auto) a = collections::StackArray<int, 5>{1, 2, 3, 4, 5};
auto val = a.get(6);
Run Code Online (Sandbox Code Playgroud)

我提供了一个文字 (6),所以我认为应该在编译时检查该值。更重要的是,如果我尝试获取调用的用户输入.get(),代码也可能拒绝编译。

auto in;
cin >> in;
a.get(in)  // Wrong!
Run Code Online (Sandbox Code Playgroud)

我想,但也会限制潜在的操作,例如循环数组并使用该.get()方法。即使如此,也可以使用下标运算符(无需进行边界检查)。

c++ assertion

0
推荐指数
1
解决办法
206
查看次数

如何在 cypress 中断言 ::before

我有一个 ag-grid,如链接 https://www.ag-grid.com/example/所示

在此输入图像描述

当我将银行余额过滤到 1114 时在此输入图像描述

此过滤器图标出现在银行余额之前

假设我没有任何其他选项,除了::before通过添加和删除过滤器值**出现和消失

<span class="ag-icon ag-icon-filter" unselectable="on" role="presentation">
::before
</span>
Run Code Online (Sandbox Code Playgroud)

**

那么我如何::before仅对关键字进行断言

assertion cypress

0
推荐指数
1
解决办法
385
查看次数

尝试循环遍历 Cypress 中的 span 标签列表,但断言失败

我试图循环浏览span标签列表,然后断言视频上显示的所有三个时间戳都是正确的。当我在 Cypress 中运行测试时,出现以下错误:

预计 [] 包括 0:20

在尝试做出第一个断言后它就崩溃了。

我编写的测试可能过于复杂,但我想看看是否有人可以通过查看我的代码并找出问题所在来提供帮助。我编写测试的方法是循环遍历元素列表,将每个值存储在空数组中,然后断言它们。

describe("Asserting timestamps", () => {
  beforeEach(() => {
    myLoginData
  })

  it("Asserting the right timestamps are displaying in Video", () => {  
    displayedTimeStamp = []; 
    cy.get(".timestamp").each((element) => {
      expect(element).to.exist;
      cy.wrap(element);
      .invoke("text")
      .then((element) => {
        displayedTimeStamp.push(element[0].innerText);
      });
    })

    expect(displayedTimeStamp).includes("0:20") || 
    expect(displayedTimeStamp).includes("0:25") ||
    expect(displayedTimeStamp).includes("0:45")
  });
});
Run Code Online (Sandbox Code Playgroud)
<div class="video-timestamp-editor"
  <div class="video-notes-list"
    <div class=video-note-data">
      <span class="timestamp">0:20</span>
      <span class="video-note-author">Alexander the Great</span>
      <span class="note-text">" This is a note "</span>
    </div>
    <div class=video-note-data">
      <span class="timestamp">0:25</span>
      <span class="video-note-author">Alexander …
Run Code Online (Sandbox Code Playgroud)

javascript arrays assertion cypress

0
推荐指数
1
解决办法
129
查看次数

你能让 assertTrue 错误消息更具体吗?

我经常使用 testNG 的 assertTrue 来验证交易是否正确

public void verifyAmount(WebElement element, String someText){
assertTrue(element.getText().contains(someText));
}
Run Code Online (Sandbox Code Playgroud)

当它失败时,它说

java.lang.AssertionError: did not expect to find [true] but found[false]
Run Code Online (Sandbox Code Playgroud)

是否可以更改断言错误以说明究竟出了什么问题,而不仅仅是真/假陈述?是否可以使该消息更具体?有没有办法让断言错误说:

java.lang.AssertionError: did not expect to find [10.000 $] but found[3000 $]
Run Code Online (Sandbox Code Playgroud)

java testng assert assertion appium

-1
推荐指数
2
解决办法
565
查看次数