当使用GHCi时,我想知道如何-Wall在(重新)从提示符加载时使用该选项.
例如,在Haskell编程提示 的3.3节中,带有防护装置的示例如下:
-- Bad implementation:
fac :: Integer -> Integer
fac n | n == 0 = 1
| n /= 0 = n * fac (n-1)
-- Slightly improved implementation:
fac :: Integer -> Integer
fac n | n == 0 = 1
| otherwise = n * fac (n-1)
Run Code Online (Sandbox Code Playgroud)
它说:"第一个问题是编译器几乎不可能检查这样的警卫是否是详尽无遗的,因为警卫条件可能是任意复杂的(如果你使用-Wall选项,GHC会警告你)."
我知道我可以ghci -Wall some_file.hs从命令行键入但是在提示符中我不确定如果我想重新加载,如何检查警告.
尝试Google之后,我似乎无法找到答案!
提前致谢!
我正在编写一些单元测试,以确保用户模型的密码长度不超过8个字符.
我开始使用User模型:
class User < ActiveRecord::Base
...
validates :password, :length =>{
:minimum => 90,
:too_short => "password is too short, must be at least %{count} characters"
}, :on => :create
end
Run Code Online (Sandbox Code Playgroud)
和user_spec.rb测试:
describe User do
subject { FactoryGirl.build :user }
its(:password) { should have_at_least(8).items }
end
Run Code Online (Sandbox Code Playgroud)
但是我意识到这实际上并没有测试我的验证,只是测试我的工厂有一个> = 8个字符的密码.
除了测试有效之外,还有一种很好的方法吗?0-7字符密码的方法?
我的理论是,如果我只测试7个字符,并且有人意外硬编码,那4个字符的密码就可以了,这将通过验证,但实际上不是预期的.可能有一些代码,其中取决于密码超过8个字符(不太可能,但在其他情况下可能是真的),因此允许密码4是不正确的.
在这种情况下,在模型中更改密码验证的人不会知道他们做错了什么.
我只想知道如何使用TDD正确测试这样的情况.
我对Rails和Rspec有点新意,因此我不确定如何在我的模型中测试日期时间验证是否正确.我做了一个有开始和结束时间的模型事件,并且在这些事件上有一些重要条件,例如开始时间不能在过去,结束时间必须在开始时间之后.
为了确保这些验证我正在使用ValidatesTimeliness https://github.com/adzap/validates_timeliness
我的模型如下:
class Event < ActiveRecord::Base
...
validates_datetime :start_date,
:after => :now,
:after_message => "Event cannot start in the past"
validates_datetime :end_date,
:after => :start_date,
:after_message => "End time cannot be before start time"
end
Run Code Online (Sandbox Code Playgroud)
在我的RSpec测试中,我有:
describe Event do
let(:event) { FactoryGirl.build :event }
subject { event }
context "when start_date is before the current time" do
it {should_not allow_value(1.day.ago).
for(:start_date)}
end
context "when end_date is before or on start date" do
it {should_not allow_value(event.start_date - 1.day).
for(:end_date)} …Run Code Online (Sandbox Code Playgroud) 我在使用PrepareForTest注释和创建AmazonSQSClient的新实例时遇到了一些麻烦.
我正在写一个Jenkins插件,不幸的是需要模拟FormValidation静态类,以确保在我的插件的字段验证时产生警告和错误消息.但是当创建一个AmazonSQSClient我得到的实例时org.apache.http.conn.ssl.SSLInitializationException
我把它抽象成一个非常简单的例子,这是我的测试文件:
package com.test;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
public class SQSTest {
// This is for mimicking the mocking of FormValidation
private static class Foo {}
@Test
@PrepareForTest(Foo.class)
public void buildTest()
{
AmazonSQS sqs = new AmazonSQSClient();
}
}
Run Code Online (Sandbox Code Playgroud)
运行此代码时出现以下错误:
org.apache.http.conn.ssl.SSLInitializationException: Failure initializing default SSL context
at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:360)
at org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(SSLSocketFactory.java:175)
at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:49)
at org.apache.http.impl.conn.PoolingClientConnectionManager.<init>(PoolingClientConnectionManager.java:93)
at com.amazonaws.http.ConnectionManagerFactory.createPoolingClientConnManager(ConnectionManagerFactory.java:26)
at com.amazonaws.http.HttpClientFactory.createHttpClient(HttpClientFactory.java:87)
at com.amazonaws.http.AmazonHttpClient.<init>(AmazonHttpClient.java:121)
at com.amazonaws.AmazonWebServiceClient.<init>(AmazonWebServiceClient.java:66)
at …Run Code Online (Sandbox Code Playgroud) 我最近阅读了有关在汇编中使用printf和scanf的这篇文章:
特别是它说"在printf中,换行符打印换行符然后(如果输出处于行缓冲模式,它可能是),刷新内部输出缓冲区,以便您可以实际看到结果.所以当你删除10 ,没有同花顺,你看不到输出."
但是,如果我在汇编文件中输出后不想要换行,我不知道该怎么办.这是一个简单的测试文件,我写的是尝试打印而没有换行:
extern printf
LINUX equ 80H ; interupt number for entering Linux kernel
EXIT equ 60 ; Linux system call 1 i.e. exit ()
section .data
int_output_format: db "%ld", 0
segment .text
global main
main:
mov r8, 10
push rdi
push rsi
push r10
push r9
mov rsi, r8
mov rdi, int_output_format
xor rax, rax
call printf
pop r9
pop r10
pop rsi
pop rdi
call os_return ; return to operating system
os_return:
mov rax, EXIT …Run Code Online (Sandbox Code Playgroud) 我想避免模拟类的getClass()方法,但似乎无法找到它的任何方法.我正在尝试测试一个将HashMap中的对象类类型存储到以后要使用的特定方法的类.一个简短的例子是:
public class ClassToTest {
/** Map that will be populated with objects during constructor */
private Map<Class<?>, Method> map = new HashMap<Class<?>, Method>();
ClassToTest() {
/* Loop through methods in ClassToTest and if they return a boolean and
take in an InterfaceA parameter then add them to map */
}
public void testMethod(InterfaceA obj) {
final Method method = map.get(obj.getClass());
boolean ok;
if (method != null) {
ok = (Boolean) method.invoke(this, obj);
}
if (ok) {
obj.run();
}
} …Run Code Online (Sandbox Code Playgroud) 我是Android编程的新手,并希望从创建一个非常基本的应用程序开始,该应用程序在屏幕上显示当前位置的纬度和经度.
我正在为我的三星Galaxy S3开发并读取你需要启动手机才能返回GPS.
我已尝试onConnected()在requestLocationUpdates()方法调用方法调用方法中执行此操作.但是我发现LocationClient仍然返回一个null位置.
谁能告诉我我做错了什么?
这是我的MainActivity:
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private LocationClient mLocationClient;
private Location mCurrentLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient = new LocationClient(this, this, this);
}
@Override
protected void onStart() {
super.onStart();
mLocationClient.connect();
}
@Override
protected void onStop() …Run Code Online (Sandbox Code Playgroud) 我正在尝试定义一些在其中调用printf的子例程.一个非常简单的例子如下:
extern printf
LINUX equ 80H
EXIT equ 60
section .data
intfmt: db "%ld", 10, 0
segment .text
global main
main:
call os_return ; return to operating system
os_return:
mov rax, EXIT ; Linux system call 60 i.e. exit ()
mov rdi, 0 ; Error code 0 i.e. no errors
int LINUX ; Interrupt Linux kernel
test:
push rdi
push rsi
mov rsi, 10
mov rdi, intfmt
xor rax, rax
call printf
pop rdi
pop rsi
ret
Run Code Online (Sandbox Code Playgroud)
这里测试只是调用printf,将数字10输出到屏幕.我不希望这个被调用,因为我没有打电话给它.
但是在编译和运行时:
nasm …Run Code Online (Sandbox Code Playgroud) 我注意到Java Enum文档说明了序数方法:
返回此枚举常量的序数(它在枚举声明中的位置,其中初始常量的序数为零).大多数程序员都没有使用这种方法.它设计用于复杂的基于枚举的数据结构,例如EnumSet和EnumMap.
我理解在线的所有示例都建议不要使用ordinal索引到数组而是使用索引EnumMap.特别是有效Java的第33项
但我的问题是:在我的Enum定义中使用它是否可以?例如我的代码如下:
public enum Direction {
NORTH(0, 1), NORTH_EAST(1, 1), EAST(1, 0), SOUTH_EAST(1, -1),
SOUTH(0, -1), SOUTH_WEST(-1, 1), WEST(-1, 0), NORTH_WEST(-1, 1);
private final int xOffset;
private final int yOffset;
private final static int DEGREES = 360;
private Direction(int xOffset, int yOffset) {
this.xOffset = xOffset;
this.yOffset = yOffset;
}
public Position move(Position position) {
return new Position(position.getX() + xOffset, position.getY() + yOffset);
}
public Direction rotate(int degrees) …Run Code Online (Sandbox Code Playgroud) 嗨,我正在尝试编写一些使用printf打印给定字符串的汇编代码.我在.data部分中使用之前声明我的字符串,测试示例如下所示:
extern printf
extern fflush
LINUX equ 80H ; interupt number for entering Linux kernel
EXIT equ 60 ; Linux system call 1 i.e. exit ()
section .data
outputstringfmt: db "%s", 0
sentence0: db "Hello\nWorld\n", 0
segment .text
global main
main:
mov r8, sentence0
push r8
call print_sentence
add rsp, 8
call os_return
print_sentence:
push rbp
mov rbp, rsp
push r12
mov r12, [rbp + 16]
push rsi
push rdi
push r8
push r9
push r10
mov rsi, r12
mov …Run Code Online (Sandbox Code Playgroud)