assert

Should I be using assert in my PHP code?

一个人想着一个人 提交于 2019-11-27 11:51:58
A coworker has added the assert command a few times within our libraries in places where I would have used an if statement and thrown an exception. (I had never even heard of assert before this.) Here is an example of how he used it: assert('isset($this->records); /* Records must be set before this is called. */'); I would have done: if (!isset($this->records)) { throw new Exception('Records must be set before this is called'); } From reading the PHP docs on assert , it looks like it's recommended that you make sure assert is active and add a handler before using assert. I can't find a place

Python这些问题你会吗?

怎甘沉沦 提交于 2019-11-27 11:46:00
inal作用域的代码一定会被执行吗? 正常的情况下,finally作用域的代码一定会被执行的,不管是否发生异常。哪怕是调用了sys.exit函数,finally也是会被执行的,那怎么样才能让finally代码不执行了。 上面的代码主要是通过让流程停滞在try作用域里,从而实现了需求。上面的代码不排除有点投机取巧的意思,但是我们实习了题目的需求不是吗。 可以对含有任意的元素的list进行排序吗? 正常情况下: 那是不是以为着,任何list都可以调用sort函数进行排序了? python里1j是一个特殊符号代表-1的平方根,出现这个问题的原因是sort函数调用的对象的__lt__函数来比较两个对象的,而复杂的数字类型是不可比较的,也就说没有实现__lt__函数,所以比较不了。因此,对于list里包含的对象如果都是可以比较的,也就是说实现了__lt__函数,那么对list调用sort函数是没问题的。 Python可是使用++x或者x++之类的操作吗? ++x操作是可以的,但是这个操作产生的结果和C语言里该操作产生的结果是不一样的,Python里++x操作里的加好只是一个一元操作符,所以,++x等价于+(+x),所以++x == x。 x++操作是不合法的,虽然有些情况下,x++看着是合法的,比如:x++-y,但其实这个表达式等价于x+(+(-y)) = x-y,所以正常情况下,x+

Exception vs. error-code vs. assert

孤者浪人 提交于 2019-11-27 11:16:52
问题 I'm working on a library that generates reports of devices. The generate_report (const std::string& no) member function can fail due to various reasons: invalid report no. invalid state (the report_generator is a FSM) no device is active error during report generation Which error-handling mechanism is best for these errors? just return true or false return error code assert and log throw exception(s) any combination of the above Some context information: the normal workflow is as following.

Why doesn't JUnit provide assertNotEquals methods?

天大地大妈咪最大 提交于 2019-11-27 08:56:42
问题 Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods? It provides assertNotSame (corresponding to assertSame ) and assertFalse (corresponding to assertTrue ), so it seems strange that they didn't bother including assertNotEqual . By the way, I know that JUnit-addons provides the methods I'm looking for. I'm just asking out of curiosity. 回答1: I'd suggest you use the newer assertThat() style asserts, which can easily describe all kinds of

assertEquals(obj,obj) returns a failed test

倖福魔咒の 提交于 2019-11-27 08:40:32
问题 Hmm, I have a money object that allows me to add other money objects into it. I tried assertEquals() in java for testing out if my code if okay, but then it failed. I'm very positive that my code is correct ( System.out.println returns the correct answer), I think I'm just using assertEquals in the wrong manner. T_T What exactly do I use if I want to find out if myObj1 == myObj2 for the tests? **in my test.java** assertEquals(new Money(money1.getCurrency(),new Value(22,70)),money1.add(money2)

Reliably determine the number of elements in an array

孤街醉人 提交于 2019-11-27 08:39:40
Every C programmer can determine the number of elements in an array with this well-known macro: #define NUM_ELEMS(a) (sizeof(a)/sizeof 0[a]) Here is a typical use case: int numbers[] = {2, 3, 5, 7, 11, 13, 17, 19}; printf("%lu\n", NUM_ELEMS(numbers)); // 8, as expected However, nothing prevents the programmer from accidentally passing a pointer instead of an array: int * pointer = numbers; printf("%lu\n", NUM_ELEMS(pointer)); On my system, this prints 2, because apparently, a pointer is twice as large as an integer. I thought about how to prevent the programmer from passing a pointer by

Android NDK assert.h problems

主宰稳场 提交于 2019-11-27 06:55:19
问题 First one - is what NDEBUG somehow already defined by default, so asserts don't work until you #undef it. Second one - they do they work, but i receive no logging in DDMS. If there is some android specific one assert.h? Or i just do something wrong? 回答1: If you want to compile your code with asserts then you can do it in three ways: use NDK_DEBUG=1 argument in ndk-build commandline add android:debuggable="true" to < application > tag in AndroidManifest.xml add APP_OPTIM := debug to your

How do I check (at runtime) if one class is a subclass of another?

[亡魂溺海] 提交于 2019-11-27 06:46:27
Let's say that I have a class Suit and four subclasses of suit: Heart, Spade, Diamond, Club. class Suit: ... class Heart(Suit): ... class Spade(Suit): ... class Diamond(Suit): ... class Club(Suit): ... I have a method which receives a suit as a parameter, which is a class object, not an instance. More precisely, it may receive only one of the four values: Heart, Spade, Diamond, Club. How can I make an assertion which ensures such a thing? Something like: def my_method(suit): assert(suit subclass of Suit) ... I'm using Python 3. You can use issubclass() like this assert issubclass(suit, Suit) .

differences between 2 JUnit Assert classes

这一生的挚爱 提交于 2019-11-27 05:59:58
The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is? The classes I'm referring to are: junit.framework.Assert and org.junit.Assert . Mnementh The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase . That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way. Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase . But that

Assert.AreEqual fails while it shouldn't

老子叫甜甜 提交于 2019-11-27 05:45:07
问题 I have a really weird behavior which I cannot explain. I have the following class: public class Project { public virtual int Id { get; set; } public virtual string Name { get; set; } } And a method which returns a Project object: public Project GetByName(string Name) { using (ISession session = NHibernateHelper.OpenSession()) { Project project = session.CreateCriteria(typeof(Project)) .Add(Restrictions.Eq("Name", Name)) .UniqueResult<Project>(); return project; } } I have added a Unit Test to